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后面的表达式运算后执行的(此时并没 ...
随机推荐
- 流媒体传输协议之 RTP (上篇)
本系列文章将整理各个流媒体传输协议,包括 RTP/RTCP,RTMP,希望通过深入梳理协议的设计细节,能够给流媒体领域的开发者带来一定的启发. 作者:逸殊 审核:泰一 介绍 RTP,即 real-ti ...
- Atlas 读写分离 & Atlas + MHA 故障自动恢复
目录 Atals 介绍 Atlas 主要功能 Atlas 相对于官方 MySQL-Proxy 的优势 Atlas 使用 Atlas 安装 Atlas 目录 Atlas 配置 Atlas 启动 Atla ...
- Linux-源码安装及FPM打包
目录 源码安装 制作RPM包(使用FPM工具) 安装rpm后要执行的脚本(优化版) 源码安装 这里举例Nginx的源码安装,需要前往Nginx官网找到稳定版本源码安装包下载. ## 源码安装nginx ...
- 2018牛客多校第一场 E-Removal【dp】
题目链接:戳这里 转自:戳这里 题意:长度为n的序列,删掉m个数字后有多少种不同的序列.n<=10^5,m<=10. 题解:dp[i][j]表示加入第i个数字后,总共删掉j个数字时,有多少 ...
- Splunk监控软件操作
一. Splunk公司与产品 美国Splunk公司,成立于2004年,2012年纳斯达克上市,第一家大数据上市公司,荣获众多奖项和殊荣.总部位于美国旧金山,伦敦为国际总部,香港设有亚太支持中心,上海 ...
- USB2.0协议学习笔记---USB工作过程(类的方法)
前面学习了那么多的概念,这里需要记住一点分层概念即设备 ---> 配置 ---> 接口 ---> 端点,这种分层的概念结构 . 也可以理解为端点构成接口,接口组成配置,配置组成设备. ...
- Smashing Conf 2020
Smashing Conf 2020 https://smashingconf.com/online-workshops/ events https://smashingconf.com/ny-202 ...
- Online analog video interview
Online analog video interview 在线模拟视频面试 English 口语 https://www.pramp.com/#/ https://www.pramp.com/faq ...
- React useMemo
React useMemo react hooks https://reactjs.org/docs/hooks-reference.html#usememo useCallback & us ...
- taro & Block
taro & Block https://nervjs.github.io/taro/docs/children.html#注意事项-1 import Taro, { Component, E ...