本文研究的主要是Java中finally和return的关系,具体介绍和实例如下所示。
finally 和 return 关系的总结
1.try块中有System.exit(0)这样的语句,由于它是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。
2.其它情况下,finally语句都必然会被执行。因此可以在这里执行一些资源的释放操作。
(1)finally中的return会覆盖try/catch中的renturn。
(2)在finally中写return语句会有警告,因为它会阻止函数抛出异常,而改为正常返回。
package com.demo.test;
public class FinallyAndReturn {
private static void finallyAndTryReturn() {
try {
System.out.println("finallyAndTryReturn -> try");
return;
}
catch (Exception e) {
System.out.println("finallyAndTryReturn -> catch");
}
finally {
System.out.println("finallyAndTryReturn -> finally");
}
}
private static void finallyAndCatchReturn() {
try {
System.out.println("finallyAndCatchReturn -> try");
throw new Exception();
}
catch (Exception e) {
System.out.println("finallyAndCatchReturn -> catch");
return;
}
finally {
System.out.println("finallyAndCatchReturn -> finally");
}
}
// finally语句是在try的return语句执行之后,return返回之前执行。
private static String tryReturn() {
String str = "initialized";
try {
System.out.println("tryReturn -> try");
str = "try";
return str;
}
catch (Exception e) {
System.out.println("tryReturn -> catch");
str = "catch";
}
finally {
System.out.println("tryReturn -> finally");
str = "finally";
}
return null;
}
private static String tryReturnAndFinallyReturn() {
String str = "initialized";
try {
System.out.println("tryReturnAndFinallyReturn -> try");
str = "try";
return str;
}
catch (Exception e) {
System.out.println("tryReturnAndFinallyReturn -> catch");
str = "catch";
}
finally {
System.out.println("tryReturnAndFinallyReturn -> finally");
/*
* Warning: finally block does not complete normally
* 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
*/
str = "finally";
return str;
}
}
private static String tryThrowAndFinallyReturn() throws Exception {
String str = "initialized";
try {
System.out.println("tryThrowAndFinallyReturn -> try");
str = "try";
throw new Exception();
}
catch (Exception e) {
System.out.println("tryThrowAndFinallyReturn -> catch");
str = "catch";
throw new Exception();
}
finally {
System.out.println("tryThrowAndFinallyReturn -> finally");
/*
* Warning: finally block does not complete normally
* 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
*/
str = "finally";
return str;
}
}
private static void finallyAndRuntimeException() {
try {
System.out.println("finallyAndRuntimeException -> try");
throw new RuntimeException();
}
catch (Exception e) {
System.out.println("finallyAndRuntimeException -> catch");
}
finally {
System.out.println("finallyAndRuntimeException -> finally");
}
}
private static void finallyAndExit() {
try {
System.out.println("finallyAndExit -> try");
// System.exit(0);是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。
System.exit(0);
}
catch (Exception e) {
System.out.println("finallyAndExit -> catch");
}
finally {
System.out.println("finallyAndExit -> finally");
}
}
public static void main(String[] args) {
finallyAndTryReturn();
System.out.println();
finallyAndCatchReturn();
System.out.println();
System.out.println("tryReturn return -> " + tryReturn());
System.out.println();
System.out.println("tryReturnAndFinallyReturn return -> " + tryReturnAndFinallyReturn());
System.out.println();
try {
System.out.println("tryThrowAndFinallyReturn return -> " + tryThrowAndFinallyReturn());
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println();
finallyAndRuntimeException();
System.out.println();
finallyAndExit();
}
}
演示结果: