一篇很好的关于异常处理的文章:http://www.blogjava.net/freeman1984/archive/2007/09/27/148850.html 收藏到这里供以后参考
另外一些个人心得:
1.抛出异常后(throw异常),当前的执行路径被中止,后面的代码(除此try,catch对应的finally中的)就不会执行下去了
2.如果异常没有被抛出的话,那么try,catch,finally处理完异常后,还会继续执行程序后面的代码。
3.finally中的代码无论之前是否throw了都会被执行到。
以上可通过下面小例子验证:
class A {
public void b(String s) {
int a;
try {
a = Integer.parseInt(s);
System.out.println(a);
} catch (NumberFormatException e) {
e.printStackTrace();
// throw e;后面的代码(除此try,catch对应的finally中的)就不会执行了 //可以注释以查看throw与否的区别
} finally {
System.out.println("error");
}
try {
a = Integer.parseInt(s);
} catch (NumberFormatException e) {
e.printStackTrace();
} finally {
System.out.println("error");
}
}
}
public class ExceptionTest {
public static void main(String[] args) {
A a = new A();
a.b("a");
}
}
945

被折叠的 条评论
为什么被折叠?



