The Artima Developer Community
Under The Hood | Book List | Print | Email | First Page | Previous | Next
Sponsored Link

Try-finally Clauses Defined and Demonstrated
Java Bytecodes that Deal with Finally Clauses
by Bill Venners
First Published in JavaWorld, February 1997

Page 1 of 3  >>

Advertisement

Summary
All Java programs are compiled into class files that contain bytecodes, the machine language of the Java virtual machine. This article takes a look at the way finally clauses are handled by the Java virtual machine, including an examination of the relevant bytecodes.


Welcome to another installment of Under The Hood. This column gives Java developers a glimpse of the mysterious mechanisms clicking and whirring beneath their running Java programs. This month's article continues the discussion of the bytecode instruction set of the Java virtual machine (JVM). Its focus is the manner in which the JVM handles finally clauses and the bytecodes that are relevant to these clauses.

Finally: Something to cheer about
As the Java virtual machine executes the bytecodes that represent a Java program, it may exit a block of code -- the statements between two matching curly braces -- in one of several ways. 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.

To use a try-finally clause:

For example:

try {
      // Block of code with multiple exit points
}
finally {
      // Block of code that is always executed when the try block is exited,
      // no matter how the try block is exited
}

If you have any catch clauses associated with the try block, you must put the finally clause after all the catch clauses, as in:

try {
      // Block of code with multiple exit points
}
catch (Cold e) {
      System.out.println("Caught cold!");
}
catch (APopFly e) {
      System.out.println("Caught a pop fly!");
}
catch (SomeonesEye e) {
      System.out.println("Caught someone's eye!");
}
finally {
      // Block of code that is always executed when the try block is exited,
      // no matter how the try block is exited.
      System.out.println("Is that something to cheer about?");
}

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:

Caught cold!
Is that something to cheer about?

Page 1 of 3  >>

Under The Hood | Book List | Print | Email | First Page | Previous | Next

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us