之前很少这样写,所以对这方面的体验很少。现在自己证实了一把,确认throws exception之后,不会有任何的返回。这个感觉有点异样,惭愧,惭愧!!程序直接中断!验证的代码如下:
public class ThrowsException {
public String devideByZero() throws Exception {
try {
int i = 1 / 0;
System.out.println("---------------2--------------");
} catch (Exception e) {
throw new Exception();
} finally{
System.out.println("---------------finally--------------");
}
System.out.println("---------------3--------------");
return "2";
}
/**
* @param args
*/
public static void main(String[] args) {
ThrowsException te = new ThrowsException();
String kk = null;
try {
kk = te.devideByZero();
System.out.println(kk);
} catch (Exception e) {
System.out.println("-------------4---------------");
System.out.println("The third line:"+kk);
}
System.out.println("The second line:"+kk);
}
}
本文通过实例解析Java中抛出异常后的程序中断现象,详细解释了如何正确使用try-catch-finally结构处理异常,并揭示了异常抛出与捕获之间的微妙关系。
319

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



