This article mainly studies the relationship between finally and return in Java. The specific introduction and examples are shown below.
1. There is a statement like System.exit(0) in the try block. Since it terminates the Java virtual machine JVM, even the JVM is stopped and all ends. Of course, finally statements will not be executed.
2. In other cases, finally statements will inevitably be executed. Therefore, some resource release operations can be performed here.
(1) The return in finally will override the return in try/catch.
(2) There will be a warning when writing a return statement in finally, because it will prevent the function from throwing an exception and return it normally.
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");}}// The final statement is executed after the return statement of the try, and before the return is returned. 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 * If the return statement is included in the finally block, even if the previous catch block retags, the statement calling the method will not obtain the exception retags by the catch block, but will get the return value of the finally block and will not catch the exception. */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 * If the finally block contains a return statement, even if the previous catch block re-throws the exception, the statement calling the method will not obtain the exception re-throwed by the catch block, but will get the return value of the finally block and will not catch the exception. */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); terminates the Java virtual machine JVM. Even the JVM is stopped, and everything is finished. Of course, finally statements will not be executed. 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();}}Demonstration results:
The above is the entire content of this article about the analysis of finally and return in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!