There are at least two situations in which the finally statement will not be executed:
(1) The try statement is not executed. If return is returned before the try statement, the finally statement will not be executed. This also shows that the necessary but not sufficient condition for the finally statement to be executed is: the corresponding try statement must be executed.
(2) There is a statement like System.exit(0); in the try block|catch block. System.exit(0) terminates the Java virtual machine JVM. Even the JVM is stopped and everything is over. Of course, the finally statement will not be executed.
In try-catch-finally, when return encounters finally, return is invalid for finally, that is:
1. When return is made in try catch block, finally will also be executed.
2. The return statement in finally will overwrite the effect of the return statement in the try catch block.
Conclusion: The return statement is not necessarily the exit of the function. When the return is executed, the value after the return is just copied to the return value variable.
class Exc{ int a; int b; } public class Except { @SuppressWarnings("finally") static int compute (){ Exc e = new Exc(); e.a = 10; e.b = 10; int res = 0 ; try{ res = e.a / e.b; System.out.println("try ……"); return res + 1; }catch(NullPointerException e1){ System.out.println("NullPointerException occured"); }catch(ArithmeticException e1){ System.out.println("ArithmeticException occured"); }catch(Exception e3){ System.out.println("Exception occured"); }finally{ System.out.println("finnaly occured"); } System.out.println(res); return res+3; } public static void main(String[] args){ int b = compute(); System.out.println("mian b= "+b); } }
Output:
try …… finnaly occured mian b= 2
Conclusion: If there is no exception, execute the code block in try until the return in try, then execute the code block in finally. After finally is executed, return to try Execute return. Exit function.
class Exc{ int a; int b; } public class Except { @SuppressWarnings("finally") static int compute (){ Exc e = new Exc(); // e.a = 10; // e.b = 10; int res = 0 ; try{ res = e.a / e.b; System.out.println("try ……"); return res + 1; }catch(NullPointerException e1){ System.out.println("NullPointerException occured"); }catch(ArithmeticException e1){ System.out.println("ArithmeticException occured"); }catch(Exception e3){ System.out.println("Exception occured"); }finally{ System.out.println("finnaly occured"); } System.out.println(res); return res+3; } public static void main(String[] args){ int b = compute(); System.out.println("mian b= "+b); } }
Output:
ArithmeticException occured finnaly occured 0 mian b= 3
Conclusion: If there is an exception in try, jump to the exception code block captured by catch at the exception statement. After executing catch, execute finally and jump out of try{}catch{} finally{}, continue to execute downwards, and the subsequent statements in try will not be executed.
The above is the detailed content of Detailed explanation of examples of finally statement in java. For more information, please follow other related articles on the PHP Chinese website!