3.13. Compiling finally

(This section assumes a compiler generates class files with version number 50.0 or below, so that the jsr instruction may be used. See also §4.10.2.5.)

Compilation of a try-finally statement is similar to that of try-catch. Prior to transferring control outside the try statement, whether that transfer is normal or abrupt, because an exception has been thrown, the finally clause must first be executed. For this simple example:

void tryFinally() {    try {        tryItOut();    } finally {        wrapItUp();    }}

the compiled code is:

Method void tryFinally()0   aload_0             // Beginning of try block1   invokevirtual #6    // Method Example.tryItOut()V4   jsr 14              // Call finally block7   return              // ...

Get The Java® Virtual Machine Specification, Java SE 7 Edition, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.