当前位置:网站首页>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.
边栏推荐
- Real-time image acquisition based on FPGA
- The application of AI in the whole process of medical imaging equipment
- LeetCode 每日一题 2022/7/25-2022/7/31
- Detailed explanation of STP election (step + case)
- Layer 2 broadcast storm (cause + judgment + solution)
- Installation of mysql5.7.37 under CentOS7 [perfect solution]
- Project (5) - Small target detection tph-yolov5
- The whole process scheduling, MySQL and Sqoop
- Huawei od dice js
- STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
猜你喜欢
随机推荐
Problems that need to be solved by the tcp framework
StringJoiner in detail
如何搭建私有yum源
CMOS和TTL的区别?
分布式与集群是什么 ? 区别是什么?
【C语言】进制转换一般方法
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
Observer mode (1)
Installation, start and stop of redis7 under Linux
经典链表OJ强训题——快慢双指针高效解法
字体压缩神器font-spider的使用
Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
The simulation application of common mode inductance is here, full of dry goods for everyone
数学解决——环形链表问题
YOLOV5 study notes (2) - environment installation + operation + training
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
YOLOV5学习笔记(三)——网络模块详解
Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
Draw Your Cards