How does finally work in java




















Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Output inside try block 17 finally : i execute always. Output inside try block catch : exception handled. For one, the JVM simply could execute past the closing curly brace of the block of code.

Or, it could encounter a break, continue, or return statement that causes it to jump out of the block of code from somewhere in the middle of the block. Finally, an exception could be thrown that causes the JVM either to jump to a matching catch clause, or, if there isn't a matching catch clause, to terminate the thread. With these potential exit points existing within a single block of code, it is desirable to have an easy way to express that something happened no matter how a block of code is exited.

In Java, such a desire is expressed with a try-finally clause. If you have any catch clauses associated with the try block, you must put the finally clause after all the catch clauses, as in:. If during execution of the code within a try block, an exception is thrown that is handled by a catch clause associated with the try block, the finally clause will be executed after the catch clause. For example, if a Cold exception is thrown during execution of the statements not shown in the try block above, the following text would be written to the standard output:.

In bytecodes, finally clauses act as miniature subroutines within a method. At each exit point inside a try block and its associated catch clauses, the miniature subroutine that corresponds to the finally clause is called. After the finally clause completes -- as long as it completes by executing past the last statement in the finally clause, not by throwing an exception or executing a return, continue, or break -- the miniature subroutine itself returns.

Execution continues just past the point where the miniature subroutine was called in the first place, so the try block can be exited in the appropriate manner. The opcode that causes the JVM to jump to a miniature subroutine is the jsr instruction.

The jsr instruction takes a two-byte operand, the offset from the location of the jsr instruction where the miniature subroutine begins. After a miniature subroutine completes, it invokes the ret instruction, which returns from the subroutine. The ret instruction takes one operand, an index into the local variables where the return address is stored. The opcodes that deal with finally clauses are summarized in the following table:. Don't confuse a miniature subroutine with a Java method.

Java methods use a different set of instructions. Instructions such as invokevirtual or invokenonvirtual cause a Java method to be invoked, and instructions such as return , areturn , or ireturn cause a Java method to return. The jsr instruction does not cause a Java method to be invoked.

Instead, it causes a jump to a different opcode within the same method. Likewise, the ret instruction doesn't return from a method; rather, it returns back to the opcode in the same method that immediately follows the calling jsr instruction and its operands. This poses a somewhat complicated problem because writeList 's try block can exit in one of three ways. The runtime system always executes the statements within the finally block regardless of what happens within the try block.

So it's the perfect place to perform cleanup. The following finally block for the writeList method cleans up and then closes the PrintWriter. All rights reserved. Hide TOC.



0コメント

  • 1000 / 1000