Java Exception Handling (2)

Source: Internet
Author: User

From: http://blog.csdn.net/ZangXT/archive/2009/05/21/4206672.aspx
Title: Finally will definitely be executed!
In try/catch/finally statements, the finally clause is certainly executed. However, many people did different tests, but they came to different conclusions.
The specific principle is best to go to "deep into Java Virtual Machine", which provides detailed descriptions of several commands such as JSR and ret. Here, we will not analyze it in depth, but simply look at finally's features in the form of performance.

Code:
/*
* Author: Zang XT
*/
Public class testfinally {

Public static void main (string [] ARGs ){

System. Out. println ("test1:" + testfinal1 ());

System. Out. println ("Test2:" + testfinal ());

System. Out. println ("test3:" + testfin_3 ());

System. Out. println ("test4:" + testfinal4 ());

}

Static int testfinal1 (){
Int I = 1;
Try {
Return I;
} Finally {
System. Out. println ("in testfinal1 (): Finally will certainly be executed! ");
I = 48;
}
}

Static string testfinal (){
String STR = "try ";
Try {
Return STR;
} Finally {
System. Out. println ("in testfinal (): Finally will certainly be executed! ");
STR = "finally ";
}
}

Static stringbuilder testfin_3 (){
Stringbuilder build = new stringbuilder ("try ");
Try {
Return build;
} Finally {
System. Out. println ("in testfin_3 (): Finally will certainly be executed! ");
Build. append ("finally ");
Build = new stringbuilder ("guess who I am! ");
}
}

Static string testfinal4 (){
Try {
Return "Return in try ";
} Finally {
System. Out. println ("in testfinal4 (): Finally will certainly be executed! ");
Return "Return in finally ";
}
}
}

The output is:
In testfinal1 (): Finally will certainly be executed!
Test1: 1
In testfinal (): Finally will definitely be executed!
Test2: Try
In testfin_3 (): Finally will definitely be executed!
Test3: Try finally
In testfinal4 (): Finally will certainly be executed!
Test4: Return in finally

The conclusion is obvious that the finally statement is indeed executed and must be executed before the return method. In addition, if there is a return statement in Finally, the method ends directly. Note that the return statement in try will push the returned result value to the stack, and then transfer it to the finally subprocess. After the finally subprocess is executed (no return ), then return.

Here are four examples:
In testfinal1 (), return I; pushes the result I value, that is, 1, to the stack. Even if I is modified (I = 48) in finally, it does not affect 1 that has been pushed into the stack.

In testfinal (), return STR; pushes STR content into the stack. For example, we assume that STR content is 0x108 (only an address value ), we can find "try" through this address value, and the content in the stack is 0x108. Run STR = "finally". At this time, the content of the STR variable may change to 0x237, which is the address of the string "finally. What is returned after a method call is completed? 0x108 pushed into the stack at return. Therefore, when printing the result, we print the string "try" found through 0x108 ".

In testfin_3 (), the return pressure stack is the value of the build variable, for example, 0x3579. Through this value, we can find the stringbuilder object. The finally statement block modifies the content of this object. Build = new stringbuilder ("guess who I am! "); Let the build variable point to a new object. At this time, the build value may be 0x4579. However, don't forget that the original stringbuilder object is still at 0x3579, And we press the stack at 0x3579! After the method is returned, the returned value 0x3579 is obtained. The corresponding stringbuilder object is found through this reference value, so the printed result is test3: Try finally.

In testfinal4 (), finally has a return statement, which returns directly and ends with the method.

Why do different people come to different conclusions? The key is not to correctly understand what the pressure stack is. In fact, when you are a beginner in Java, if you understand what variables are and distinguish between references and objects, you will not get a wrong conclusion. In addition, if we understand that in Java, method calls all adopt the value passing mode, we can also understand it here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.