java通过异常处理错误_一天一个Java基础——通过异常处理错误

《Thinking in Java》上对这章的讲解不少,可见重要性,学习和总结一些主要的记录下来。

一、创建自定义异常

1 packageException;2

3 class SimpleException extendsException{}4

5 public classInheritingException{6

7 public void f() throwsSimpleException {8 System.out.println("Throw SimpleException from f()");9 throw newSimpleException();10 }11

12 public static voidmain(String[] args) {13 InheritingException sed = newInheritingException();14 try{15 sed.f();16 } catch(SimpleException e) {17 e.printStackTrace();18 }19 }20

21 }

1 输出:2 Throw SimpleException from f()3 Exception.SimpleException4 at Exception.InheritingException.f(InheritingException.java:10)5 at Exception.InheritingException.main(InheritingException.java:19)

throw与throws的区别与详情

编译器创建了默认构造器,它将自动调用基类的默认构造器。

对异常来说,最重要的部分就是类名,其它也没用,可以增加一个带参的构造方法。

比如NullPointerException:

1 public

2 class NullPointerException extendsRuntimeException {3 private static final long serialVersionUID = 5162710183389028792L;4

5 /**

6 * Constructs a {@codeNullPointerException} with no detail message.7 */

8 publicNullPointerException() {9 super();10 }11

12 /**

13 * Constructs a {@codeNullPointerException} with the specified14 * detail message.15 *16 *@params the detail message.17 */

18 publicNullPointerException(String s) {19 super(s);20 }21 }

二、捕获异常

1)try块

如果在方法内部抛出了异常(或者在方法内部调用的其他方法抛出了异常),这个方法将在抛出异常的过程中结束。

要是不希望方法就此结束,可以在方法内设置一个特殊的块来捕获异常。

try{

//exceptions

}

2)异常处理程序

异常处理程序紧跟在try块之后,以关键字catch表示:

try{

//exceptions

} catch(Type1 id1) {

//Type1

} catch(Type2 id2) {

//Type2

}

当异常被抛出时,异常处理机制将负责搜寻参数与异常类型相匹配的第一个处理程序。然后进入catch子句执行,此时认为异常得到了处理。

注意,只有匹配的catch子句才能得到执行,这与switch语句不同。

3)栈轨迹

printStackTrace()方法所提供的信息可以通过getStackTrace()方法来直接访问,这个方法将返回一个由栈轨迹中的元素所构成的数组,其中每一个元素都表示

栈中的一帧。元素0是栈顶元素,并且是调用序列中的最后一个方法调用。数组中最后一个元素和栈底是调用序列中的第一个方法调用。

1 public classWhoCalled {2 static voidf() {3 try{4 throw newException();5 } catch(Exception e) {6 for(StackTraceElement ste : e.getStackTrace()) {7 System.out.println("line: " + ste.getLineNumber() + " method: " +ste.getMethodName());8 }9 }10 }11 static voidg() {f();}12 static voidh() {g();}13 public static voidmain(String[] args) {f();g();h();}14 }

程序输出:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

line: 5method: f

line:14method: main

line:5method: f

line:12method: g

line:14method: main

line:5method: f

line:12method: g

line:13method: h

line:14 method: main

View Code

三、Java标准异常

Throwable这个Java类被用来表示任何可以作为异常被抛出的类。

Throwable对象可分为两种类型:

Error用来表示编译时和系统错误。

Exception是可以被抛出的基本类型,程序员关心的基类型通常是Exception。

四、RuntimeException

if(t == null) {throw newNullPointerException();

}

如果对Null引用进行调用,Java会自动抛出NullPointerException异常,所以上述代码是多余的,它属于Java的标准运行时检测的一部分:

1 public classNeverCaught {2 static voidf() {3 throw newRuntimeException();4 }5 static voidg() {f();}6 public static voidmain(String[] args) {7 g();8 }9 }

1 输出:2 Exception in thread "main"java.lang.RuntimeException3 at Exception.NeverCaught.f(NeverCaught.java:6)4 at Exception.NeverCaught.g(NeverCaught.java:10)5 at Exception.NeverCaught.main(NeverCaught.java:14)

从输出可以发现,RuntimeException是一个特例,对于这种异常类型,编译器不需要异常说明,其输出被报告给了System.err。

如果RuntimeException没有被捕获而直达main(),那么在程序退出前将调用异常的printStackTrace()方法。

*注意:

只能在代码中忽略RuntimeException(及其子类)类型的异常,其它异常类型的处理都是由编译器强制实施的。

1)常见的五种RuntimeException

1 NullPointerException - 空指针引用异常2 ClassCastException - 类型强制转换异常3 IllegalArgumentException - 传递非法参数异常4 ArithmeticException - 算术运算异常5 ArrayStoreException - 向数组中存放与声明类型不兼容对象异常6 IndexOutOfBoundsException - 下标越界异常7 NegativeArraySizeException - 创建一个大小为负数的数组错误异常8 NumberFormatException - 数字格式异常9 SecurityException - 安全异常10 UnsupportedOperationException - 不支持的操作异常

六、使用finally进行清理

1 class ThreeException extendsException {}2 public classFinallyWorks {3 static int count = 0;4 public static voidmain(String[] args) {5 while(true) {6 try{7 if(count++ == 0) {8 throw newThreeException();9 }10 System.out.println("No exception");11 } catch(ThreeException e) {12 System.out.println("ThreeException");13 } finally{14 System.out.println("In finally clause");15 if(count == 2)16 break;17 }18 }19 }20 }

这个程序给了我们一些思路(确实。。),如果把try块放在循环里,就建立了一个“程序继续执行之前必须要到达”的条件。

还可以加入一个static类型的计数器或者别的装置,使循环在放弃之前能够尝试一定的次数。这将使程序的健壮性更上一个台阶(好叼的样子)。

1)finally用来做什么

当要把除内存之外的资源恢复到它们的初始状态时,就要用到finally子句。

2)在return中使用finally

因为finally子句总是会执行的,所以在一个方法中,可以从多个点返回,并且可以保证重要的清理工作仍旧会执行:

1 class ThreeException extendsException {}2 public classFinallyWorks {3 static int count = 0;4 public static voidmain(String[] args) {5 while(true) {6 try{7 if(count++ == 0) {8 throw newThreeException();9 }10 System.out.println("No exception");11 return;12 } catch(ThreeException e) {13 System.out.println("ThreeException");14 } finally{15 System.out.println("In finally clause");16 if(count == 3)17 break;18 }19 }20 }21 }

第一次循环,首先执行第7行,符合条件,抛出异常,执行catch块,最后执行finally清理,不符合第16行判断,继续循环

第二次循环,不符合第7行判断,抛出异常,并return,但依旧执行finally清理,不符合第16行判断,但try块中已经执行return,所以程序结束,输出:

ThreeException

Infinallyclause

No exception

Infinally clause

3)Java异常的缺憾:异常丢失

1 public classExceptionSilencer {2 public static voidmain(String[] args) {3 try{4 throw newRuntimeException();5 } finally{6 return;7 }8 }9 }

发现输出为空

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值