首页 新闻 会员 周边 捐助

关于java异常的一个小问题

0
[待解决问题]

Thinking in Java 12章的一个例子. 刚开始看到的现象跟预期的不一样,我稍微改了一下. 

 1 class MyException2 extends Exception{
 2     private int x;
 3     public MyException2(){
 4         super();
 5     }
 6     public MyException2(String msg){
 7         super(msg);
 8     }
 9     public MyException2(String msg,int x){
10         super(msg);
11         this.x = x;
12     }
13     public int val(){
14         return x;
15     }
16     public String getMessage(){
17         return "Detail Message: "+x+" "+super.getMessage();
18     }
19 }
20 public class ExtralFeature {
21     public static void f() throws MyException2{
22         System.out.println("Throwing MyExcetion2 from f()");
23         throw new MyException2();
24     } 
25     public static void g() throws MyException2{
26         System.out.println("Throwing MyExcetion2 from g()");
27         throw new MyException2("Originated in g()");
28     } 
29     public static void h() throws MyException2{
30         System.out.println("Throwing MyExcetion2 from h()");
31         throw new MyException2("Originated in h()",47);
32     } 
33      
34     public static void main(String[] args) {
35         for(int i=0;i<5;i++){
36             try {
37                 f();
38             } catch (MyException2 e) {
39                 // TODO: handle exception
40                 e.printStackTrace();
41             }
42             try {
43                 g();
44             } catch (MyException2 e) {
45                 // TODO: handle exception
46                 e.printStackTrace();
47             }
48              
49             try {
50                 h();
51             } catch (MyException2 e) {
52                 // TODO: handle exception
53                 e.printStackTrace();
54                 System.out.println("e.val="+e.val());
55             }
56             System.out.println("========================================");
57         }
58     }
59 }

一个关于自定义异常的小例子. 刚开始我理解的,,mian方法中,执行到f()方法的时候,抛出异常,程序暂停,进入catch块中去处理. 完事儿之后返回,继续往下执行下一个try块中的g()方法,g()方法抛出异常之后,进入g()方法的catch块处理...所以我理解的输出应该是这个样子的

Throwing MyExcetion2 from f()
ch12_Exception.MyException2: Detail Message: 0 null
at ch12_Exception.ExtralFeature.f(ExtralFeature.java:25)
at ch12_Exception.ExtralFeature.main(ExtralFeature.java:39)
Throwing MyExcetion2 from g()
ch12_Exception.MyException2: Detail Message: 0 Originated in g()
at ch12_Exception.ExtralFeature.g(ExtralFeature.java:29)
at ch12_Exception.ExtralFeature.main(ExtralFeature.java:45)
Throwing MyExcetion2 from h()
ch12_Exception.MyException2: Detail Message: 47 Originated in h()
at ch12_Exception.ExtralFeature.h(ExtralFeature.java:33)
at ch12_Exception.ExtralFeature.main(ExtralFeature.java:52)
e.val=47
========================================
...
...
...

 

但是看输出结果貌似不是这个样子的,实际的输出是:

Throwing MyExcetion2 from f()
Throwing MyExcetion2 from g()
Throwing MyExcetion2 from h()
e.val=47
========================================
Throwing MyExcetion2 from f()
Throwing MyExcetion2 from g()
Throwing MyExcetion2 from h()
e.val=47
========================================
Throwing MyExcetion2 from f()
Throwing MyExcetion2 from g()
ch12_Exception.MyException2: Detail Message: 0 null
at ch12_Exception.ExtralFeature.f(ExtralFeature.java:25)
at ch12_Exception.ExtralFeature.main(ExtralFeature.java:39)
ch12_Exception.MyException2: Detail Message: 0 Originated in g()
at ch12_Exception.ExtralFeature.g(ExtralFeature.java:29)
at ch12_Exception.ExtralFeature.main(ExtralFeature.java:45)
ch12_Exception.MyException2: Detail Message: 47 Originated in h()
at ch12_Exception.ExtralFeature.h(ExtralFeature.java:33)
...
...
...

 

所以就有点疑惑了,看上去像是多线程在执行.  刚学java,不是很懂. 是我理解错了吗? 还是说正确的应该是,main线程一直在执行,遇到f()方法抛出异常,将catch块交给另外一个线程,然后main这个线程会继续往下执行. 

不愿透露姓名的高杨的主页 不愿透露姓名的高杨 | 菜鸟二级 | 园豆:202
提问于:2016-09-26 00:23
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册