有关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,如需转载请自行联系原作者
目录
相关文章
|
存储 物联网 数据管理
使用Apache IoTDB进行IoT相关开发的架构设计与功能实现(12)
现在到了使用Apache IoTDB进行IoT相关开发的架构设计与功能实现的最后一个环境,在本文中我将向大家介绍IoTDB的查询语言。IoTDB为咱们广大开发者提供了类似SQL的查询语言,用于与IoTDB进行交互,查询语言可以分为4个主要部分:架构语句、数据管理语句、数据库管理语句、功能。
607 0
|
6月前
|
Linux 数据安全/隐私保护 Windows
CentOS-6.3-x86_64-minimal 安装教程详细步骤新手入门指南(附安装包)
准备2G以上U盘及ISO刻录工具,下载CentOS 6.3 minimal版镜像,使用Rufus或dd写入U盘。将U盘插入目标电脑,通过BIOS选择U盘启动,进入安装界面后按提示选择语言、键盘布局,自动分区并设置主机名、网络及时区,配置root密码后开始安装。安装完成后重启,拔出U盘,以root账号登录系统,即可使用命令行进行操作。
746 157
|
11月前
|
消息中间件 监控 数据可视化
实时看板数据分析的力量:推动高效决策的17个关键维度
实时看板数据分析通过可视化工具将企业运营数据实时整合展示,助力快速决策。它结合数据收集、处理与交互分析,广泛应用于制造、零售、金融等领域,提升响应速度与协作效率。技术上依赖流数据处理与可视化工具,如Power BI、Tableau等,同时关注数据安全与用户体验优化。
642 1
|
6月前
|
数据采集 机器学习/深度学习 算法
智慧交通大数据分析
智慧交通大数据分析通过多源数据采集、实时计算与智能算法,实现交通态势感知、拥堵预测、信号优化及应急响应,提升通行效率与出行体验。结合实战案例,展现其在缓解拥堵、提高管理效能方面的显著成效,推动城市交通智能化发展。(238字)
457 0
|
存储 人工智能 安全
有奖体验 AI 模特换装,解锁电商视觉新体验
在电商中,制作精美的商品展示图成本高且流程复杂。AI 换装技术允许商家快速更换模特的服装或配件,无需重新拍摄,大大缩短准备时间。这项技术减少了对专业摄影师和后期团队的依赖,使中小商家也能轻松产出高质量的商品图片,灵活响应市场变化,有效降低成本,提升竞争力。本方案利用函数计算 FC 构建 Web 服务,采用百炼视觉模型 qwen-vl-max-latest、aitryon、aitryon-refiner、shoemodel-v1 来分别实现 AI 人物主体信息提取、模特试衣、试衣精修、模特换鞋。
|
人工智能
如何使用chatGPT生成小红书种草文案
小红书拥有超千万的日活用户,为商家提供了广阔的变现空间和机会,成为商家选择在小红书上推广的原因之一。 小红书种草文案,例如具有影响力的热门话题、产品使用方法等内容可以让消费者迅速了解产品为品牌带来更多新客户。想要在小红书实现种草,离不开种草文案。今天就教大家使用ChatGPT撰写小红书种草文案。
929 0
|
数据中心
Cat5 与 Cat5e:两种网线的区别和比较
Cat5 与 Cat5e:两种网线的区别和比较
1427 1
Cat5 与 Cat5e:两种网线的区别和比较
|
前端开发 安全 JavaScript
怎么判断bug是前端问题还是后端问题?
怎么判断bug是前端问题还是后端问题?
903 0
|
存储 编解码 编译器
iOS项目Project 和 Targets配置详解
最近开始学习完整iOS项目的开发流程和思路,在实际的项目开发过程中,我们通常需要对项目代码和资料进行版本控制和管理,一般比较常用的SVN或者Github进行代码版本控制和项目管理。
1180 0
|
算法
基于MATLAB的ASK,FSK,PSK误码率对比仿真,输出调制后波形以及误码率曲线
基于MATLAB的ASK,FSK,PSK误码率对比仿真,输出调制后波形以及误码率曲线
1652 0
基于MATLAB的ASK,FSK,PSK误码率对比仿真,输出调制后波形以及误码率曲线