Try_catch_finally Everyone should use a lot, many people also know some of their execution order, but it really is so simple, the following we first look at a piece of code, we think about his execution results?
Public class testexception { intGoabl=1; Public testexception() { }intTestEx ()throwsException {intRET =1;Try{ret++;returnRet }Catch(Exception e) {ret++;returnRet }finally{ret=0; } }intTESTEX1 ()throwsException {intRET =1;Try{ret++;intt=ret/(ret-2);//Make a exception returnRet }Catch(Exception e) {ret++;returnRet }finally{ret=0; } }intTESTEX2 ()throwsException {intRET =1;Try{ret++;intt=ret/(ret-2);//Make a exception returnRet }Catch(Exception e) {ret++;returnRet }finally{ret=0;returnRet } }intTESTEX3 ()throwsException {intRET =1;Try{ret++;returnRet }Catch(Exception e) {ret++;returnRet }finally{ret=0;returnRet } }intTESTEX4 ()throwsException {Try{goabl++;returnGOABL; }Catch(Exception e) {goabl++;returnGOABL; }finally{goabl=0; } } Public Static void Main(string[] args) {TestException TestException1 =NewTestException ();Try{System.out.println (Testexception1.testex ()); System.out.println (Testexception1.testex1 ()); System.out.println (Testexception1.testex2 ()); System.out.println (Testexception1.testex3 ()); System.out.println (Testexception1.testex4 ()); System.out.println (GOABL); }Catch(Exception e) {E.printstacktrace (); } } }
Okay, now we're going to announce the answer:
2
3
0
0
2
0
If your answer is correct and you know why, then you don't have to look at the content below.
First of all, I will not elaborate on the Java exception handling mechanism, want to understand the students can go to see Java exception processing in-depth study.
We begin by explaining the order in which try_catch_finally is executed.
First of all, I can clearly tell you that the execution order of try_catch_finally is very simple, the first is to execute a try in the statement if there is an exception directly into the catch, finally will be executed.
So someone might ask, why is the first output not 0? Don't worry. Let's take a note.
TestEx
The try statement does not have any exceptions so the catch is not executed. RET performed a + + operation, then the program is returned, in memory will generate a temporary variable to save the RET, that is, ret value assigned to a temporary variable so, ret in finally is assigned to zero and does not change the returned value so the output is 2;
TestEx1
This time we created the exception in the try statement so that the program will go into the catch, so RET is + + two times, the same finally in the statement can not affect the return value, so the output is 3;
TestEx2
In relation to testEx1 we add a return statement to the finally statement, saying that the return value of the function becomes 0, so that the name can be determined after the return value of the function is finally done.
TestEx3
Same as testEx2. The return value is 0 because finally is added to the return statement;
TestEx4
In this function we change the object of the operation to a global variable, and with the last output we can know that the return result of the function is actually received by a temporary variable, so the function returns 2, but the GOABL has changed to 0.
Ok this time to this, feel good classmate help me in below a praise Bai, on the bottom of the blog, click on that ding Zi just fine, thank you.
Copyright NOTICE: This article is the original blog article, reproduced please indicate the source
Try_catch_finally of "Interview plus sub-item" Java Exception handling