1. Debug track code:
Public Static Void Entertainment () {System.out.println ("Enter after Try Field"); ("Enter Catch Field");} Public Static Void EnterfinallyMethod () {{) System.out.println ("Enter Finally Method");2. Throwing Exception, no finally, when Catch meets Return
Public static int Catchtest () {int res = 0; try {res = 10 /0; // throw the exception, follow -up processing is rejected by Entertainment (); Meet } Catch (Exception E) {EnteRExceptionMethod (); Return 1; // Exception, get the chance of calling method and return method value}}}}}}}}Background output results:
Enter Catch Field 1
3. Throw the Exception. When there is Return in the Catch body, the code block of Finally will be executed before Catch executes Return
Public static int Catchtest () {int res = 0; try {res = 10 /0; // throw the exception, follow -up processing is rejected by Entertainment (); Meet } Catch (Exception E) {EnteRExceptionMethod (); Return 1; // Exception is thrown, the chance of getting the method of calling and returning the method value} Finally {EnterfinallyMethod (); // Exception throw it out, F Inlly code will execute Return in Catch Before being executed}}Background output results:
Enter Catch Field Enter Finally Method 1
4. Do not throw the Exception. When the finally code block encounters Return, the final method will be over after finishing
Public static int Catchtest () {int res = 0; try {res = 10 /2; // Do not throw the Exception Entertainment (); Return res; // Get the opportunity to be executed, but the execution needs Executive} Catch (Exception E) {EntertainmentMethod (); Return 1;} Finally {EnterfinallyMethod (); Return 1000; // Finally contains Return statement, this Return will end This method will not jump back after executing TRY or CATCH continues to execute, and the method end here}}Background output results:
Enter after Field Enter Finally Method 1000
5. Do not throw Exception, when the finals of the Finally code block, the system.exit () method will end and terminate the entire program, not just the method
Public static int Catchtest () {int res = 0; try {res = 10 /2; // Do not throw the Exception Entertainment (); Return res; // get the chance of being executed, but because Finally has terminated the program, the return value No chance to be returned} Catch (Exception E) {EntexceptionMethod (); Return 1;} Finally {EnterfinalityMethod (); System.exit (0); // Finally contains System.exit. () System, system.exit () will Exit the entire program, the program will be terminated}}Background output results:
Enter after Field Enter Finally Method
6. Throwing Exception. When Catch and Finally encounters Return at the same time, the return value of Catch will not be returned. Finally Return statement will end the entire method and return
public static int catchTest() { int res = 0; try { res = 10 / 0; // 抛出Exception,后续处理将被拒绝enterTryMethod(); return res; // Exception已经抛出,没有获得被执行的Opportunities} Catch (Exception E) {EntertainmentMethod (); Return 1; // Exception has been thrown out and gets the opportunity to be executed, but the return operation will be cut by final} Finally {EnterfinallyMethod (); Return 10; // Return will end The entire method, the return value is 10}}Background output results:
Enter Catch Field Enter Finally Method 10
7. Do not throw the Exception. When Finally encounters Return, Try's Return return value will not be returned, the Finally RETURN statement will end the entire method and return to the entire method and return
Public static int catchtest () {int res = 0; try {res = 10 /2; // Do not throw the Exception Entertainment (); Return resonten; Exception E ) {EntexceptionMethod (); Return 1;} Finally {EnterfinallyMethod (); Return 10; // Return will end the entire method, the return value is 10}}}Background output results:
Enter after Field Enter Finally Method 10
in conclusion
In the abnormal processing of Java, after the program executes the code block in TRY, this method will not end immediately. Instead, it will continue to try to find the method of the method of this method.
If there is no Finally code block, the entire method returns the corresponding value after the TRY code block is executed to end the entire method. If there is a finalized code block, when the program executes the Return in the TRY code block, Return will not be executed immediately. Instead
If there is no RETURN in the Finally code block or the code that can not terminate the program, the program will return the TRY code block to execute the RETURN statement after performing the Finally code block code to end the entire method. If there is a return in the final code block or the code containing the code that can terminate the program, the method will be ended after the endAlly is executed.
In the case of throwing abnormalities, the principle is the same as above. It is OK to change the TRY try to Catch above.