Before reading the following content, I would like to ask you a question:
Will finally statements be executed?
Use the integer. valueof method to simulate an exception to illustrate the problem between try catch and finally.
Example 1: no exception, normal
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("2");} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");} finally {System.out.println("finally statement :-)");}}}
Example 2: no exception, normal, but add return in the try statement
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("2");return;} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");} finally {System.out.println("finally statement :-)");}}}
The running results of Example 1 and Example 2 are the same:
Example 3: Manufacturing exception
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("bluetooth");} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");} finally {System.out.println("finally statement :-)");}}}
Example 4: create an exception. Add return to the catch statement.
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("bluetooth");} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");return;} finally {System.out.println("finally statement :-)");}}}
The running result of Example 3 and Example 4 is the same:
From this, we can see that the finally statements are executed in the above four cases.
Example 5: create an exception. Add return to the catch statement.
Purpose: To verify whether the code behind the finally code block is executed ??????
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("bluetooth");} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");return;} finally {System.out.println("finally statement :-)");}System.out.println("out of try...catch...finally statement");}}
Execution result:
If you comment out the return in the catch statement, the execution result is as follows:
Example 6: create an exception. Add return to the try statement.
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("dd");return;} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");} finally {System.out.println("finally statement :-)");}System.out.println("out of try...catch...finally statement");}}
Execution result:
Example 7: no exception. Add return in the try statement.
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("2");return;} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");} finally {System.out.println("finally statement :-)");}System.out.println("out of try...catch...finally statement");}}
Execution result (no exception, return returns, so the statement after the finally code block will not execute :-)
The above indications indicate:
All finally statements have been executed, but there is another situation that has not been verified. Next let's look at it.
Exit JVM in try... catch:
public class TryCatchFinally {public static void main(String[] args) {try {System.out.println("try statement :-)");Integer.valueOf("2eer");System.exit(0);} catch (NumberFormatException nfe) {System.out.println("catch statement :-)");System.exit(0);} finally {System.out.println("finally statement :-)");}System.out.println("out of try...catch...finally statement");}}
Execution result:
If system. Exit is called in a try or catch Statement (any one), the finally statement will not be executed.