public class TestException { public static void main(String[] args) {
try {
oneMethod();
} catch (Exception e) {
System.out.println("there is an exception");
} } static void oneMethod() {
try {
twoMethod();
System.out.println("normal");
} catch (ArithmeticException e) {
System.out.println("there is an arithmeticException");
} finally {
System.out.println("in finally");
}
System.out.println("outside try block"); } static void twoMethod() {
throw new NullPointerException();
}}这个程序的结果是:
in finally
there is an exception
我困惑的是,结果为什么不是
in finally
outside try block
there is an exception
请大家来看看,帮忙解释下
解决方案 »
- 为什么JPanel无法浏览图片。。。
- java filewriter 与 outputstream 问题
- java初学者学习期间发现的两个小程序不明白请教大虾
- 有偿转让Java即时通信工具源程序及设计文档
- 怎样获得.jar文件的txt文本文件
- 请问java怎样判断一个操作系统文件是否存在?
- 关于RMI的简单问题
- 请问具体介绍一下visual cafe吗??有下载的地方吗?多大?有卖的吗?
- 请问java中有类似vc的atof和atoi的语句吗?
- 关于Javascript....请高手指点!
- 如何用jfreechart画出三个长方形区域,望各位高手指点,十分感谢!
- java+netbeans编一个ping的小程序,请高手指教
但是catch块里并没有catch这类型的异常,所以方法oneMethod执行完finally里的语句
后就会终止,并将异常往上层抛。
而没有定义子类catch的话程序就会抛向
catch (Exception e)
然后做finally,下一步就终止了..不会去做 System.out.println("outside try block")
在mian方法的最后一句的话是会输出outside try block的
public static void main(String[] args) {
try {
oneMethod();
} catch (Exception e) {
System.out.println("there is an exception");
}
System.out.println("Exception has handled in OneMethod() fuction,");
System.out.println("It's no need to hand again in the main fuction");
}
catch (ArithmeticException e) {
System.out.println("there is an arithmeticException");
}
/*此处捕获NullPointException异常,经此处理System.out.println("outside try block");就会执行*/
catch(NullPointerException ex){
System.out.println("this is a NullPointException");
}
finally {
System.out.println("in finally");
}
输出:
this is a NullPointException
in finally
outside try block
Exception has handled in OneMethod() fuction,
It's no need to hand again in the main fuction
try {
oneMethod();
} catch (Exception e) {
System.out.println("there is an exception");
}
System.out.println("Exception has handled in OneMethod() fuction,");
System.out.println("It's no need to hand again in the main fuction");
} static void oneMethod() {
try {
twoMethod();
System.out.println("normal");
} catch (ArithmeticException e) {
System.out.println("there is an arithmeticException");
} catch (NullPointerException ex) {
System.out.println("this is a NullPointException");
} finally {
System.out.println("in finally");
}
System.out.println("outside try block"); } static void twoMethod() {
throw new NullPointerException();
}}
的结果为:
this is a NullPointException
in finally
outside try block
Exception has handled in OneMethod() fuction,
It's no need to hand again in the main fuction而当去掉上面带颜色的语句之后(即: catch (NullPointerException ex) {
System.out.println("this is a NullPointException");
} )
结果为:
in finally
there is an exception
Exception has handled in OneMethod() fuction,
It's no need to hand again in the main fuction
我不太明白的是去了带色的语句后为什么不打印outside try block????????
你应该debug一下,eclipse会清楚的告诉你程序每一部执行了什么。
如果oneMethod这个方法中捕捉到了twoMethod抛出的NullPointerException,那么执行完finally后就会执行System.out.println("outside try block");
但是这里oneMethod这个方法中没有捕获到twoMethod抛出的NullPointerException,那么执行完finally就会停止,因为他在try里面抛出了异常但是没有被捕捉到,那么就等于
说这个try catch块没有用,所以当然跳出try catch快后就会停止(因为他会抛出个异常啊)。我把程序改成这样你就会明白了
public class TestException {
public static void main(String[] args) {
// try {
oneMethod();
// } catch (Exception e) {
// System.out.println("there is an exception");
// } } static void oneMethod() {
try {
twoMethod();
System.out.println("normal");
} catch (ArithmeticException e) {
System.out.println("there is an arithmeticException");
} finally {
System.out.println("in finally");
}
System.out.println("outside try block"); } static void twoMethod() {
throw new NullPointerException();
}}这样的话
in finally
Exception in thread "main" java.lang.NullPointerException
at Test12_27.TestException.twoMethod(TestException.java:27)
at Test12_27.TestException.oneMethod(TestException.java:15)
at Test12_27.TestException.main(TestException.java:6)
这是输出。
现在该明白了吧。
当发现异常后回回到mian方法中去
当异常处理完毕后不会再回到oneMethod()方法了,而是继续执行mian方法
所以就不再输出outside try block.
不知道我理解的对不对,请达人指点!