1. Debug tracking code:
public static void Entertrymethod () {
System.out.println ("Enter after try Field");
}
public static void Enterexceptionmethod () {
System.out.println ("Enter catch field");
}
public static void Enterfinallymethod () {
System.out.println (' Enter finally method ');
}
2. Throw exception, no finally, when catch meets return
public static int catchtest () {
int res = 0;
try {
res = 10/0;//Throw exception, subsequent processing is rejected
Entertrymethod ();
return res; Exception has been thrown, not getting executed opportunity
} catch (Exception e) {
enterexceptionmethod ();
return 1; Exception thrown, obtaining an opportunity to invoke the method and return the method value
}
Background output results:
3. Throw exception, when a block of code with a return,finally body in the catch is executed before the catch executes return
public static int catchtest () {
int res = 0;
try {
res = 10/0;//Throw exception, subsequent processing is rejected
Entertrymethod ();
return res; Exception has been thrown, not getting executed opportunity
} catch (Exception e) {
enterexceptionmethod ();
return 1; Exception thrown, obtaining the opportunity to invoke the method and return the value of the method
} finally {
enterfinallymethod ();//Exception thrown, Finally code will be executed before the catch executes return
Background output results:
Enter Catch field
Enter finally method
1
4. Do not throw exception, when finally code block inside encounter return,finally execution will end the whole method
public static int catchtest () {
int res = 0;
try {
res = 10/2;//Do not throw exception
Entertrymethod ();
return res; Get an opportunity to be executed, but execution needs to be performed after finally execution is completed
} catch (Exception e) {
enterexceptionmethod ();
return 1;
} finally {
enterfinallymethod ();
return 1000; Finally contains the return statement, which ends this method, does not jump back to try or catch execution after execution, method to this end
}
Background output results:
Enter after try field
Enter finally method
1000
5. Do not throw exception, when finally code block inside encounter System.exit () method will end and terminate the entire program, not just method
public static int catchtest () {
int res = 0;
try {
res = 10/2;//Do not throw exception
Entertrymethod ();
return res; Get the opportunity to be executed, but because finally has terminated the program, the return value has no chance to be returned
} catch (Exception e) {
enterexceptionmethod ();
return 1;
} finally {
enterfinallymethod ();
System.exit (0); Finally contains the System.exit () statement, System.exit () will exit the entire program, the program will be terminated
}
Background output results:
Enter after try field
Enter finally method
6. Throw exception, when catch and finally also encounter Return,catch return value will not be returned, finally return statement will end the entire method and returns
public static int catchtest () {
int res = 0;
try {
res = 10/0;//Throw exception, subsequent processing will be rejected
Entertrymethod ();
return res; Exception has been thrown, not getting executed opportunity
} catch (Exception e) {
enterexceptionmethod ();
return 1; Exception has been thrown to get the chance of being executed, but the return operation will be finally truncated
} finally {
enterfinallymethod ();
return 10; Return will end the entire method with a value of {}
}
Background output results:
Enter Catch field
Enter finally method
10
7. Do not throw exception, when finally met Return,try return value will not be returned, finally return statement will end the entire method and returns
public static int catchtest () {
int res = 0;
try {
res = 10/2;//Do not throw exception
Entertrymethod ();
return res; Get an execution opportunity, but the return will be finally truncated
} catch (Exception e) {
enterexceptionmethod ();
return 1;
} finally {
enterfinallymethod ();
return 10; Return will end the entire method with a value of {}
}
Background output results:
Enter after try field
Enter finally method
10
Conclusions
in Java exception handling, after the program executes the code block in the try, the method does not end immediately, but continues to try to find out if the method has a finally code block
If there is no finally code block, the entire method returns the corresponding value after executing the try code block to end the entire method
If there is a finally code block, then the program executes the return sentence in the try code block without immediately executing return, but first executes the code in the finally code block.
If the finally code block does not return or there is no code to terminate the program, the program returns the try block code after executing the finally code block to execute the back statement to end the entire method. If a finally code block has return or code that has the ability to terminate the program, the method ends after the execution of finally, and no longer jumps back to the try code block to perform
In the case of throwing an exception, the principle is the same as the above, you can change the try to catch to understand the OK.