当前位置:网站首页>try...finally总结
try...finally总结
2022-07-24 12:09:00 【沐风游云】
一、测试代码
代码1:
public class Test1 {
public static void main(String[] args) {
System.out.println(new Test1().test());;
}
public static int test(){
int x = 1;
try {
x++;
return x;
}finally {
++x;
}
}
}
运行结果:2
代码2:
public class Test2 {
public static void main(String[] args) {
System.out.println(new Test2().test());;
}
public static int test()
{
int x = 1;
try {
x++;
return x;
}finally {
++x;
return x;
}
}
}
运行结果:3
代码3:
class Num {
public int x = 1;
}
public class Test3 {
public static void main(String[] args) {
System.out.println(new Test3().test());;
}
public static int test(){
Num x = new Num();
try {
x++;
return x;
}finally {
++x;
}
}
}
运行结果:3
二、结论
1、finally块中代码都会执行;
2、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,不管finally中的代码怎么样,返回的值都不会改变,仍然是之前保存的值),所以函数返回值是在finally执行前确定的;
3、finally中改变数据类型为引用数据类型的数据时,返回值改变(因为引用数据类型地址不变);
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。在实际项目中,finally常常是用来关闭流或者数据库资源的,并不额外做其他操作。
边栏推荐
- L2-011 玩转二叉树
- [mathematical basis of Cyberspace Security Chapter 3] congruence
- Leetcode:51. queen n
- C#入门系列(二十九) -- 预处理命令
- MySQL creates partition tables and automatically partitions them by day
- Wechat official account development: Material Management (temporary and permanent)
- js图像转base64
- TypeNameExtractor could not be found
- One of his birds sold for 60million -- the collection of eight mountain people in the Ming and Qing Dynasties
- How to use a third party without obtaining root permission topic: MIUI chapter
猜你喜欢

Three small knowledge points about data product managers

Share the typora tool

2 万字详解,吃透 ES!

GCC的基本用法

第1章 引言

基于ARM和FPGA的数字示波器设计——QMJ

Basic usage of GCC
![[C and pointer Chapter 14] preprocessor](/img/da/a9a15299157389f8738f7c642a9ff7.png)
[C and pointer Chapter 14] preprocessor

JMeter while controller
![[rust] rust language foundation | you should quickly get an impression when learning a language](/img/70/b420d62759f0c52bf4cc8d2a500813.png)
[rust] rust language foundation | you should quickly get an impression when learning a language
随机推荐
Why is the datetime type field 8 hours earlier when I use flinkcdc to read MySQL's binlog?
Svn server and client installation (Chinese package) and simple use
L1-043 阅览室
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
Makefile quick use
Wechat official account development: Material Management (temporary and permanent)
Leetcode:51. queen n
Collision, removal and cleaning
Remember to optimize my personal blog once
A* and JPS
容错、熔断的使用与扩展
Counter attack dark horse: devdbops training, give you the best courses!
计算两个坐标经纬度之间的距离(5种方式)
Design of digital oscilloscope based on arm and FPGA -- QMJ
oracle 11.2.0.4 asm单实例不随系统启动而自动开启
Differences between JS map and foreach
Experience of redis deepwater area -- Interview reference
The biggest crisis for testers in the workplace is not at the age of 30, but being laid off in middle age
[mathematical basis of Cyberspace Security Chapter 3] congruence
Agile? DevOps ?