当前位置:网站首页>return in try-catch
return in try-catch
2022-07-31 02:46:00 【Fengfeng classmate】
1. Case 1 (there is return in try, but there is no return in finally):
public class TryTest{
public static void main(String[] args){
System.out.println(test());
}
private static int test(){int num = 10;try{System.out.println("try");return num += 80;}catch(Exception e){System.out.println("error");}finally{if (num > 20){System.out.println("num>20 : " + num);}System.out.println("finally");}return num;}
}
2. Case 2 (return in both try and finally):
public class TryTest{
public static void main(String[] args){
System.out.println(test());
}
private static int test(){int num = 10;try{System.out.println("try");return num += 80;}catch(Exception e){System.out.println("error");}finally{if (num > 20){System.out.println("num>20 : " + num);}System.out.println("finally");num = 100;return num;}}}
3. Case 3 (change the return value num in finally):
public class TryTest{
public static void main(String[] args){
System.out.println(test());
}
private static int test(){int num = 10;try{System.out.println("try");return num;}catch(Exception e){System.out.println("error");}finally{if (num > 20){System.out.println("num>20 : " + num);}System.out.println("finally");num = 100;}return num;}
}
4. Case 4 (wrapping the value of num in the Num class):
public class TryTest{
public static void main(String[] args){
System.out.println(test().num);
}
private static Num test(){Num number = new Num();try{System.out.println("try");return number;}catch(Exception e){System.out.println("error");}finally{if (number.num > 20){System.out.println("number.num>20 : " + number.num);}System.out.println("finally");number.num = 100;}return number;}
}
class Num{
public int num = 10;
}
1.
The output result is as follows:
try
num>20 : 90
finally
90
Analysis: Obviously "return num += 80" is split into two statements "num = num+80" and "return num", the line executes the "num = num+80" statement in try,Save it, before the "return num" in try is executed, execute the statement in finally, and then return 90.
2.
The output is as follows:
try
num>20 : 90
finally
100
Analysis: The return statement in try is also split, and the return statement in finally is executed before the return statement in try, so the return statement in try is "overwritten" and is no longer executed.
3.
The output result is as follows:
try
finally
10
Analysis: Although the return value num is changed in finally, because the value of num is not returned in finally, after executing the statement in finally, the test() function will get the value of num returned in try,The value of num in try is still the value reserved before the program enters the finally code block, so the return value obtained is 10.
4.
The output is as follows:
try
finally
100
It can be seen from the results that the value of the return value num is also changed in finally. In case 3, it is not returned by the return in try (the test() method does not get 100), but hereIt was returned by the return statement in try.
Summary:
The try statement executes all other operations before returning, retains the value to be returned, and then transfers to execute the statement in finally, and then it is divided into the following three cases:
Situation 1: If there is a return statement in finally, the return statement in try will be "overwritten", and the return statement in finally will be directly executed to get the return value, so that it will not be able to get the good reserved before tryreturn value.Case 2: If there is no return statement in finally, and the return value has not been changed, after the statement in finally is executed, the return statement in try will be executed, and the previously reserved value will be returned.Case 3: If there is no return statement in finally, but the value to be returned is changed, this is somewhat similar to the difference between pass-by-reference and pass-by-value, which can be divided into the following two cases:1) If the return data is a basic data type or a text string, the changes to the basic data in finally have no effect, and the return statement in try will still return the value reserved before entering the finally block.2) If the return data is a reference data type, and the change of the attribute value of the reference data type in finally works, the return statement in try returns the value of the attribute changed in finally.
边栏推荐
- The final exam first year course
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
- Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
- 医疗影像领域AI软件开发流程
- 【Android】Room —— SQLite的替代品
- Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
- 4. Sensitive word filtering (prefix tree)
- 7. List of private messages
- 学习DAVID数据库(1)
- 19.支持向量机-优化目标和大间距直观理解
猜你喜欢
Face detection based on opencv
To write good test cases, you must first learn test design
String为什么不可变?
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
共模电感的仿真应用来了,满满的干货送给大家
Installation of mysql5.7.37 under CentOS7 [perfect solution]
Static route analysis (the longest mask matching principle + active and standby routes)
Huawei od dice js
The whole process scheduling, MySQL and Sqoop
CefSharp入门-winform
随机推荐
Multilingual settings of php website (IP address distinguishes domestic and foreign)
开题报告之论文框架
print task sorting js od huawei
Observer mode (1)
Static route analysis (the longest mask matching principle + active and standby routes)
修改未正确放入沙盒造成苹果兼容性问题
16、热帖排行
Project (5) - Small target detection tph-yolov5
What level of software testing does it take to get a 9K job?
AtCoder Beginner Contest 261 部分题解
SQL注入 Less54(限制次数的SQL注入+union注入)
8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
【银行系列第一期】中国人民银行
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
Installation, start and stop of redis7 under Linux
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care