003-try-catch-finally-return执行顺序问题
一、try-catch-finally-return执行顺序问题
0、原始执行顺序
try — > finally
try —> catch —> finally
1、try catch 中有 return,finally 中无 return,且 try 中无异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
}
}
/**
执行结果:
try: i = 2
finally: i = 5
2
*/
2、try catch finally中均有 return,try中无异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
return i;
}
}
/**
返回结果5
try: i = 2
finally: i = 5
5
*/
3、try catch 中有 return,finally 中无 return,且 try 中有异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
}
}
/**
返回结果4
exception: i = 4
finally: i = 7
4
*/
4、try catch finally中均有 return,try中有异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
return i;
}
}
/**
返回结果5
exception: i = 4
finally: i = 7
7
*/
5、try、catch 中有 return,try finally 中有异常,finally 中的异常有 return
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
try {
int b = i/0;
} catch (Exception e) {
i = i + 5;
return i;
}
System.out.println("finally: i = " + i);
}
}
/**
exception: i = 4
12
*/
6、try、catch 中有 return,try finally 中有异常,finally 中的异常无 return
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
try {
int b = i/0;
} catch (Exception e) {
i = i + 5;
}
System.out.println("finally: i = " + i);
}
}
/**
exception: i = 4
finally: i = 12
4
*/
总结
- finally 的代码总会被执行
- try、catch 中有 return 的时候,也会执行 finally。return 的时候,要注意返回值是否会受到 finally 中代码的影响。
- finally 中有 return 的时候,会直接在 finally 中退出,导致 try、catch中的 return 失效。
- finally 中如果有异常的时候,且存在 return 的时候,catch 的 return 会被覆盖。
003-try-catch-finally-return执行顺序问题的更多相关文章
- 【转】C# 异常处理 throw和throw ex的区别 try catch finally的执行顺序(return)
[转]throw和throw ex的区别 之前,在使用异常捕获语句try...catch...throw语句时,一直没太留意几种用法的区别,前几天调试程序时无意中了解到几种使用方法是有区别的,网上一查 ...
- try catch finally return运行顺序
首先让我们搞懂两组概念:try catch finally和return 1.try catch finally 首先说try catch, (1)try语句 ,try语句用来包围可能出现异常的代码片 ...
- 异常 try catch finally return 执行关系 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 有return的情况下try catch finally的执行顺序(转)
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- try catch finally的执行顺序(有return的情况下)
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- 有return的情况下try catch finally的执行顺序
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- Java基础知识强化之IO流笔记06:有return的情况下try catch finally的执行顺序
1. 给出结论: (1)不管有木有出现异常,finally块中代码都会执行:(2)当try和catch中有return时,finally仍然会执行:(3)finally是在return后面的表达式运算 ...
- 【Java疑难杂症】有return的情况下try catch finally的执行顺序
有这样一个问题,异常处理大家应该都不陌生,类似如下代码: public class Test { public static void main(String[] args) { int d1 = 0 ...
- 有return的情况下try catch finally的执行顺序(转)
结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
随机推荐
- markdown嵌入图片
这里嵌入指不会因为本地文件丢失而丢失. 参考:https://blog.csdn.net/testcs_dn/article/details/78952358 https://blog.csdn.ne ...
- 攻防世界-Web-lottery(.git泄露、php源码审计、弱类型利用)
扫描目录,发现.git泄露: 提取.git泄露的源码,得到许多文件: 网站这里: 这就要审计一下代码,找找漏洞了. 经过一番审计,猜数字对应的函数在api.php中: 我们要绕过这个$win_numb ...
- JVM系列(三):JVM创建过程解析
上两篇中梳理了整个java启动过程中,jvm大致是如何运行的.即厘清了我们认为的jvm的启动过程.但那里面仅为一些大致的东西,比如参数解析,验证,dll加载等等.把最核心的loadJavaVM()交给 ...
- CN_Week2_Neuron_code
CN_Week1_Neuron_code on Coursera Abstract for week2: -- 1. Technique for recording from the brain. - ...
- React Gatsby 最新教程
React Gatsby 最新教程 https://www.gatsbyjs.com/docs/quick-start/ https://www.gatsbyjs.com/docs/tutorial/ ...
- Google reCAPTCHA 2 : Protect your site from spam and abuse & Google reCAPTCHA 2官方教程
1
- Vue Component Registration All In One
Vue Component Registration All In One Vue 注册自定义组件 <template> <div class="back-to-top-c ...
- 微信公众号代码高亮美化工具 All In One
微信公众号代码高亮美化工具 All In One markdown 美化 mdnice 编辑器 https://www.mdnice.com/ https://github.com/mdnice ma ...
- URLSearchParams & shape URL params
URLSearchParams https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams var paramsString = ...
- js & Input & Paste & Clipboard & upload & Image
js & Input & Paste & Clipboard & upload & Image input paste upload image js Clip ...