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
请大家来看看,帮忙解释下
解决方案 »
- 求<<数据结构与问题求解 java语言描述>>(Data Structure&Problem Solving Using Java)第四版weiss著,习题答案
- swing 问题
- 关于java多线程生成文件问题
- 请问dom4j输出xml时,怎么输出&?
- 紧急求助,用Java写一通信程序
- 在jtextPane中鼠标右键无法改变jtextpane的caretposition(急)
- 关于多重继承,如何组织如下的功能
- 如何把输出流OutputStream的内容直接输出到字符串中?
- 我是新手,我来求教!请教各位达人,涉及Qos(Quality of Service)方面的知识。
- 小弟我kangta刚学java,买了本书,请前辈进来参谋下此书是否好。
- 如何用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.
不知道我理解的对不对,请达人指点!