Exception loss and return value overwrite parsing for JAVA finally words

Source: Internet
Author: User

Reprint: http://blog.csdn.net/sureyonder/article/details/5560538

The Java Virtual machine "calls" the subroutine of the finally clause at the end of each try statement block and its associated catch clause. In fact, the finally clause behaves very much like a "mini-subroutine" inside a method. After the finally clause has ended normally-that is, the last statement in the finally clause is executed properly, excluding throwing an exception, or performing a return, continue, break, and so on, the mini-subroutine that is subordinate to the finally clause performs a "return" operation. The program resumes execution of the following statement where the mini-subroutine was first called.

The finally "miniature subroutine" is not equivalent to a call to a method function, and the finally clause is executed within the same stack, and the "return" operation of the mini-subroutine does not involve the method fallback, just to make the program counter PC jump to a different location of the same method to continue execution.

An abnormal loss

[Java]View Plaincopy
  1. Public static void Exceptionlost ()
  2. {
  3. Try
  4. {
  5. Try
  6. {
  7. throw New Exception ( "Exception in Try");
  8. }
  9. finally
  10. {
  11. throw New Exception ( "Exception in finally");
  12. }
  13. }
  14. catch (Exception e)
  15. {
  16. System.out.println (e);
  17. }
  18. }

The output of Exceptionlost () is "exception in finally" rather than the exception thrown in the try block, which is a flaw in the Java exception mechanism-exception loss.

In bytecode, the throw statement is not an atomic operation. In older JDK, the throw statement of the try block in Exceptionlost () is decomposed into several steps:


1) Store the exception ("Exception in Try") object reference in a local variable
astore_2//Pop the reference to the thrown exception, store into local variable 2
2) Call finally mini-subroutine
3) Push the exception ("Exception in Try") object reference in the local variable to the top of the operand stack and throw an exception
Aload_2//Push the reference to the thrown exception from local variable 2

Athrow//Throw the exception

If finally exits by break, return, continue, or throws an exception, then the 3rd step above will not be executed.

In JDK1.6, we can see by bytecode that the finally clause is implemented as a special catch, and the following is the exception table for the Exceptionlost () method:


Exception table:
From to target type
0 any
0 Class Java/lang/exception

Finally, you can catch any type of exception that is thrown from 0 rows to 9 rows and either re-throw the caught exception, or throw a new exception that you construct, which overwrites the exception in the TRY statement block.

Two return value overrides

[Java]View Plaincopy
  1. Public static int getValue ()
  2. {
  3. int value = 0;
  4. Try
  5. {
  6. value = 100;
  7. return value;
  8. }
  9. finally
  10. {
  11. Value = 200;
  12. }
  13. }

is the return value of this method 100 or 200? The result is 100.
In bytecode, the return statement is not an atomic operation, it decomposes the return statement in GetValue () into several steps:
1) Store the value values in a local variable (named temp here):
ILOAD_0//PUSH local variable 0-the 100
istore_2//Pop an int., store into local varaible 2
2) Call finally mini-subroutine
3) Push the value of the local variable (referred to as temp) to the top of the operand stack and return to the calling method
Iload_2//Push local varaible 2-the 100
Ireturn//return int on top of the stack-the 100:return 100

Because the return statement saves the returned value to a temporary local variable before it returns, the value is re-assigned within the FINALLY clause without affecting the return value.

Understanding some of the inner knowledge of the finally clause allows us to understand what finally can and cannot do, which will help us to use the finally clause correctly.

Exception loss and return value overwrite parsing for JAVA finally words

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.