Java中finally与return的执行顺序
在 Java 的异常处理中,try、catch 和 finally 是按顺序执行的。如果 try 中没有异常,则顺序为 try→finally,如果 try 中有异常,则顺序为 try→catch→finally。但是当 try、catch、finally 中加入 return 之后,return 和 finally 的执行顺序让很多人混淆不清。下面来分别说明一下。
注意:可以使用编译器的 Debug 功能查看详细过程。如果不了解如何使用 Debug 功能可参考《Java Eclipse如何调试代码》一节。
注意:finally 代码块中最好不要包含 return 语句,否则程序会提前退出。
当返回值类型是引用类型时,结果也是一样的,代码如下:
总结为以下几条:
1. try 和 catch 中带有 return
1)try 中带有 return
public class tryDemo {
public static int show() {
try {
return 1;
} finally {
System.out.println("执行finally模块");
}
}
public static void main(String args[]) {
System.out.println(show());
}
}
输出结果如下:
执行finally模块
1
public class tryDemo {
public static int show() {
try {
int a = 8 / 0;
return 1;
} catch (Exception e) {
return 2;
} finally {
System.out.println("执行finally模块");
}
}
public static void main(String args[]) {
System.out.println(show());
}
}
输出结果为:
执行finally模块
2
注意:可以使用编译器的 Debug 功能查看详细过程。如果不了解如何使用 Debug 功能可参考《Java Eclipse如何调试代码》一节。
2. finally 中带有 return
public class tryDemo {
public static int show() {
try {
int a = 8 / 0;
return 1;
} catch (Exception e) {
return 2;
} finally {
System.out.println("执行finally模块");
return 0;
}
}
public static void main(String args[]) {
System.out.println(show());
}
}
输出结果如下:
执行finally模块
0
注意:finally 代码块中最好不要包含 return 语句,否则程序会提前退出。
3. finally 中改变返回值
下面先来看 try 代码块或者 catch 代码块中的返回值是普通变量时,代码如下:
public class tryDemo {
public static int show() {
int result = 0;
try {
return result;
} finally {
System.out.println("执行finally模块");
result = 1;
}
}
public static void main(String args[]) {
System.out.println(show());
}
}
输出结果为:
执行finally模块
0
当返回值类型是引用类型时,结果也是一样的,代码如下:
public class tryDemo {
public static Object show() {
Object obj = new Object();
try {
return obj;
} finally {
System.out.println("执行finally模块");
obj = null;
}
}
public static void main(String args[]) {
System.out.println(show());
}
}
输出结果为:
执行finally模块
java.lang.Object@15db9742
总结为以下几条:
- 当 try 代码块和 catch 代码块中有 return 语句时,finally 仍然会被执行。
- 执行 try 代码块或 catch 代码块中的 return 语句之前,都会先执行 finally 语句。
- 无论在 finally 代码块中是否修改返回值,返回值都不会改变,仍然是执行 finally 代码块之前的值。
- finally 代码块中的 return 语句一定会执行。
所有教程
- C语言入门
- C语言编译器
- C语言项目案例
- 数据结构
- C++
- STL
- C++11
- socket
- GCC
- GDB
- Makefile
- OpenCV
- Qt教程
- Unity 3D
- UE4
- 游戏引擎
- Python
- Python并发编程
- TensorFlow
- Django
- NumPy
- Linux
- Shell
- Java教程
- 设计模式
- Java Swing
- Servlet
- JSP教程
- Struts2
- Maven
- Spring
- Spring MVC
- Spring Boot
- Spring Cloud
- Hibernate
- Mybatis
- MySQL教程
- MySQL函数
- NoSQL
- Redis
- MongoDB
- HBase
- Go语言
- C#
- MATLAB
- JavaScript
- Bootstrap
- HTML
- CSS教程
- PHP
- 汇编语言
- TCP/IP
- vi命令
- Android教程
- 区块链
- Docker
- 大数据
- 云计算