有关try..catch..finally处理异常的总结

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//看一下下面的程序,你能正确的写出不同的testEx2()方法时,程序的最终打印出来的数据吗....先不要看下面的答案
public  class  ExceptionTest { 
     public  ExceptionTest() { 
    
   
     boolean  testEx()  throws  Exception { 
         boolean  ret =  true
         try 
             ret = testEx1(); 
         catch  (Exception e) { 
             System.out.println( "testEx, catch exception" ); 
             ret =  false
             throw  e; 
         finally 
             System.out.println( "testEx, finally; return value="  + ret); 
             return  ret; 
        
    
   
     boolean  testEx1(){ 
         boolean  ret =  true
         try 
             ret = testEx2();
             if (!ret){
            return  false ;
         }
             System.out.println( "testEx1, at the end of try" ); 
             return  ret;
          
         catch  (Exception e) { 
             System.out.println( "testEx1, catch exception" ); 
             ret =  false
             throw  e; 
         finally 
             System.out.println( "testEx1, finally; return value="  + ret); 
             return  ret; 
        
    
     //第一种:
    /* boolean testEx2() throws Exception{ 
         boolean ret = true; 
         try { 
 
             int b = 12; 
             int c; 
             for (int i = 2; i >= -2; i--) { 
                 c = b / i;
                 System.out.println("i=" + i); 
            
             return ret; 
         } catch (Exception e) { 
             System.out.println("testEx2, catch exception"); 
             ret = false; 
             throw e;
         } finally { 
             System.out.println("testEx2, finally; return value=" + ret); 
             return ret; 
         }
     } */
    
     
     //第二种:
      boolean  testEx2()  throws  Exception { 
         boolean  ret =  true ;  
             int  b =  12
             int  c; 
             for  ( int  i =  2 ; i >= - 2 ; i--) { 
                 c = b / i; 
                 System.out.println( "i="  + i); 
            
             System.out.printf( "这句话打打出来了吗??????" );
             return  true
       }
     
 
     //第三种:
     /*boolean testEx2() throws Exception{ 
         boolean ret = true; 
         try { 
 
             int b = 12; 
             int c; 
             for (int i = 2; i >= -2; i--) { 
                 c = b / i;
                 System.out.println("i=" + i); 
            
             return ret; 
         } catch (Exception e) { 
             System.out.println("testEx2, catch exception"); 
             ret = false; 
             throw e;
         } finally { 
             System.out.println("testEx2, finally; return value=" + ret); 
             //return ret;  //此处不写return 语句
         }
         //System.out.println("fhsdfsdofi");//因为try中有一个直接return语句,所以这两句不能被访问
         //return ret;
     } */
 
     //第四种:
     
     /*boolean testEx2() throws Exception{ 
         boolean ret = true; 
         try { 
 
             int b = 12; 
             int c; 
             for (int i = 2; i >= -2; i--) { 
                 c = b / i;
                 System.out.println("i=" + i); 
             }  
         } catch (Exception e) { 
             System.out.println("testEx2, catch exception"); 
             //ret = false; 
             throw e;
         } finally { 
             System.out.println("testEx2, finally; return value=" + ret); 
             //return ret;  //此处不写return 语句
         }
         System.out.println("这句话打印出来了!!!!");
         return ret;
     } */
     //第五种:
     
     /*boolean testEx2() throws Exception{  //提醒一下,第四种和第五种只有catch中有没有 throw e 不一样
         boolean ret = true; 
         try { 
 
             int b = 12; 
             int c; 
             for (int i = 2; i >= -2; i--) { 
                 c = b / i;
                 System.out.println("i=" + i); 
            
             //return ret; 
         } catch (Exception e) { 
             System.out.println("testEx2, catch exception"); 
             ret = false; 
             //throw e;
         } finally { 
             System.out.println("testEx2, finally; return value=" + ret); 
             //return ret;  //此处不写return 语句
         }
         System.out.println("这句话打印出来了!!!!!!!");
         return ret;
     }*/
     
     public  static  void  main(String[] args) { 
         ExceptionTest testException1 =  new  ExceptionTest(); 
         try 
             testException1.testEx(); 
         catch  (Exception e) { 
             e.printStackTrace(); 
        
    
 
class  myException  extends  Exception{
    public  void  printException(){
       System.out.println( "产生异常!" );
    }
}
 
/*异常看着容易,理解起来也很容易!但是它真的没有你想像的那么简单!
第一种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
 
第二种:
i=2
i=1
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false
 
第三种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false
 
第四种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=true
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false
 
第五种:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
这句话打印出来了!!!!!!!
testEx1, finally; return value=false
testEx, finally; return value=false
 
 
总结一下:
一:throw new Exception()
     第一种情形:(由第二种情形验证)
     int test() throws Exception{
        if(....)
           throw new Exception();
        ....
        return x;
     }
     第二中情形:第五种情况可验证
     int test() throws Exception{
        try{
          if(...)
            throw new Exception();
        }catch(ArithmeticException e){//在try中产生的异常没有被捕获时:
           ....
        }finally{
           ....
        }
        .....
        return x;
     }
    在执行到throw 时,第一种先将return返回个调用者(也就是throw和return之间的语句不会被执行),然后再调用程序中寻找处理异常的程序
    第二种还要将 finally中的语句执行完(即异常有没有被立即处理都要执行finally),(throw和return之间的语句也不会被执行),
    然后执行return语句,程序转向调用者中寻找异常处理程序。
二:再让我们看一下finally中写return的一些缺憾
1 finally块中的return语句会覆盖try块、catch块中的return语句
2 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,
而是会得到finally块的返回值,并且不会捕获异常,也就是如果在catch或者try中产生的异常如果在向外界抛出是不可能的。。。。
 
第一种情况:testEx2()方法中会产生一个ArithmeticException的异常, Exception是它的父类,我们在该方法中捕获了该异常并进行了处理
所以会输出 testEx2()中的 catch 和 finally 中的输出数据, 而在testEx1()方法中,调用了testEx2(),由于testEx2()中的异常已经被处理
并且由于finally中的return语句导致testEx2()中catch中的throw e 无法重新抛出,所以在testEx1()中不会被捕获。
 
再说一下第四,第五种情况:fianlly中都没有return
在第四种情况中, 由于在catch中继续抛出了该异常,该异常在testEx2()中没有被处理,所以在执行finally之后的语句(除了return语句)不会被执行,
而第五种是没有继续抛出该异常,也就是textEx2()中产生的异常全部被处理了,所以finally之后的语句会被执行.....其他不多说。
 
如果还是没有搞懂的话,推荐看一下下面这个链接,写的不错....
http://blog.csdn.net/hguisu/article/details/6155636
*/









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3817703.html,如需转载请自行联系原作者
目录
相关文章
|
6月前
每日一道面试题之try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?
每日一道面试题之try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?
|
3月前
|
编译器
try{...}catch(){...}finally{...}语句你真的理解吗?
try{...}catch(){...}finally{...}语句你真的理解吗?
21 0
|
15天前
|
C++
C++异常处理try和throw以及catch的使用
C++异常处理try和throw以及catch的使用
|
9月前
try catch finally,try 里面有 return,finally 还执行吗?
try catch finally,try 里面有 return,finally 还执行吗?
36 0
|
6月前
16 # 实现 catch 方法
16 # 实现 catch 方法
29 0
|
10月前
|
存储 IDE Java
try catch finally 执行顺序总结
try catch finally 执行顺序总结
88 0
|
Java 程序员 编译器
异常(Exception)
异常(Exception)
76 0
异常(Exception)
|
Java
Java异常——处理机制Try-catch-finally
Java异常——处理机制Try-catch-finally
152 0
|
Web App开发 JavaScript 前端开发
啊,似乎没有真正理解 try...catch...finally!
啊,似乎没有真正理解 try...catch...finally!
425 0
啊,似乎没有真正理解 try...catch...finally!
|
Java Spring
ConstraintViolationException最后不也抛出了,为什么没被catch到?
ConstraintViolationException最后不也抛出了,为什么没被catch到?
353 0
ConstraintViolationException最后不也抛出了,为什么没被catch到?