Java exception catch ( try catch finally ) Have you really mastered it?

http://www.blogjava.net/fancydeepin/archive/2012/07/08/java_try-catch-finally.html

Preface:
Do you really understand the exception handling mechanism in java? Have you mastered it?
How to deal with return in catch body? What should I do when the finally body encounters return? How to deal with the System.exit() method in the finally body? What should I do when return is encountered in catch and finally bodies at the same time?

I believe that when you handle an exception, you don't have to throw it every time and you're done. In many cases, we need to catch the exception and do some follow-up processing for the thrown Exception.

Go directly to the code, first paste the method to be called for the test:
1
2 // catch follow-up processing
3 public static boolean catchMethod() {
4 System.out.print("call catchMethod and return --->> ");
5 return false;
6 }
7 // finally follow-up work
8 public static void finallyMethod() {
9 System.out.println();
10 System.out.print("call finallyMethod and do something --->> ");
11 }
12


1. Throw Exception, no finally, when catch encounters return
1
2public static boolean catchTest() {
3 try {
4 int i = 10 / 0; // Exception is thrown, subsequent processing is rejected
5 System.out.println("i vaule is : " + i);
6 return true; // Exception has been thrown, no execution is obtained Opportunity
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod(); // Exception is thrown, getting the opportunity to call the method and return the method value
10 }
11 }
12

Background output result:
1
2 -- Exception --
3call catchMethod and return --->> false
4

2. Throw Exception, when there is return in catch body, the code block in finally body will be executed before catch executes return
1
2public static boolean catchFinallyTest1() {
3 try {
4 int i = 10 / 0; // Exception is thrown, subsequent processing is rejected
5 System.out.println("i vaule is : " + i);
6 return true; // Exception has been thrown, no chance to be executed
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod(); // Exception is thrown, getting the opportunity to call the method, but the method value is finally executed
10 }finally{ 11 finallyMethod
(); // Exception is thrown, finally block will be executed before catch executes return
12 }
13 }
14

The background output result:
1
2 -- Exception --
3call catchMethod and return --->> 
4call finallyMethod and do something --->> false
5

3. Do not throw Exception, when return is encountered in the finally code block, finally will end the entire method after execution
1
2public static boolean catchFinallyTest2() {
3 try {
4 int i = 10 / 2; // does not throw Exception
5 System.out.println("i vaule is : " + i);
6 return true; // Get a chance to be executed, but the execution needs to be executed after finally execution
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod ();
10 }finally{
11 finallyMethod();
12 return false; // There is a return statement in finally, this return will end the method, it will not jump back to try or catch to continue execution after execution, the method ends here, return false
13 }
14 }
15

Background output results:
1
2i vaule is : 5
3
4call finallyMethod and do something --->> false
5

4. Do not throw Exception, when the finally code block encounters the System.exit() method, it will end and terminate the entire program, not just method
1 2
public static boolean finallyExitTest() {
3 try {
4 int i = 10 / 2; // does not throw Exception
5 System.out.println("i vaule is : " + i);
6 return true; // Get a chance to be executed, but because the program has finally terminated, the return value has no chance to be returned
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true;
10 }finally {
11 finallyMethod();
12 System.exit(0);// finally contains System.exit() statement, System. .exit() will exit the whole program, the program will be terminated
13 }
14 }
15

The background output result:
1
2i vaule is : 5
3
4call finallyMethod and do something --->> 
5

5. Throw Exception, when catch and finally At the same time, when return is encountered, the return value of catch will not be returned, and the return statement of finally will end the entire method and return
1
2public static boolean finallyTest1() {
3 try {
4 int i = 10 / 0; // throw Exception , subsequent processing is rejected
5 System.out.println("i vaule is : " + i);
6 return true; // Exception has been thrown, no chance to be executed
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true; // Exception has been thrown, get a chance to be executed, but the return operation will be finally truncated
10 }finally {
11 finallyMethod();
12 return false; // return will end The whole method, returns false
13 }
14 }
15

The background output results:
1
2 -- Exception --
3 4
call finallyMethod and do something --->> false
5

6. Do not throw Exception, when finally encounters return, the return of try The return value will not be returned, the finally return statement will end the entire method and return
1
2public static boolean finallyTest2() {
3 try {
4 int i = 10 / 2; // does not throw Exception
5 System.out.println("i vaule is : " + i);
6 return true; // get executed chance, but the return will be finally truncated
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true;
10 }finally {
11 finallyMethod();
12 return false; // return will end this method, it will not jump back to try or catch to continue execution after execution, return false
13 }
14 }
15

The background output result:
1
2i vaule is : 5
3
4call finallyMethod and do something ---> > false
5

Conclusion:
(Assuming that the method needs a return value) In
the exception handling of java,
in the case of not throwing an exception, after the program executes the code block in the try, the method will not end immediately, but will continue to try to find the method Is there 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, the program executes to the end of the return statement in the try code block. If there is no return in the
finally code block or there is no code that can terminate the program, the program will return to the try code block after the code in the finally code block is executed. Execute the return statement to end the entire method;
if there is a return in the finally code block or contains code that can terminate the program, the method will be terminated after the execution of the finally, without jumping back to the try code block to execute the return.
In the case of throwing an exception, the principle is the same as above. You can replace the try mentioned above with catch to understand it. *_*

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326862966&siteId=291194637