The Artima Developer Community
Sponsored Link

How the Java Virtual Machine Handles Exceptions
A Detailed Study with Examples of Classes and Methods
by Bill Venners
First Published in JavaWorld, January 1997

<<  Page 2 of 2


Play Ball!: a Java virtual machine simulation
The applet below demonstrates a Java virtual machine executing a sequence of bytecodes. The bytecode sequence in the simulation was generated by javac for the playBall method of the class shown below:

class Ball extends Exception {

class Pitcher {

    private static Ball ball = new Ball();

    static void playBall() {
        int i = 0;
        while (true) {
            try {
                if (i % 4 == 3) {
                    throw ball;
            catch (Ball b) {
                i = 0;

The bytecodes generated by javac for the playBall method are shown below:

   0 iconst_0 // Push constant 0
   1 istore_0 // Pop into local var 0: int i = 0;
                          // The try block starts here (see exception table, below).
   2 iload_0 // Push local var 0
   3 iconst_4 // Push constant 4
   4 irem // Calc remainder of top two operands
   5 iconst_3 // Push constant 3
   6 if_icmpne 13 // Jump if remainder not equal to 3: if (i % 4 == 3) {
                          // Push the static field at constant pool location #5,
                          // which is the Ball exception itching to be thrown
   9 getstatic #5 <Field Pitcher.ball LBall;>
  12 athrow // Heave it home: throw ball;
  13 iinc 0 1 // Increment the int at local var 0 by 1: ++i;
                          // The try block ends here (see exception table, below).
  16 goto 2 // jump always back to 2: while (true) {}
                          // The following bytecodes implement the catch clause:
  19 pop // Pop the exception reference because it is unused
  20 iconst_0 // Push constant 0
  21 istore_0 // Pop into local var 0: i = 0;
  22 goto 2 // Jump always back to 2: while (true) {}

Exception table:
   from to target type
     2 16 19 <Class Ball>

The playball method loops forever. Every fourth pass through the loop, playball throws a Ball and catches it, just because it is fun. Because the try block and the catch clause are both within the endless while loop, the fun never stops. The local variable i starts at 0 and increments each pass through the loop. When the if statement is true, which happens every time i is equal to 3, the Ball exception is thrown.

The Java virtual machine checks the exception table and discovers that there is indeed an applicable entry. The entry's valid range is from 2 to 15, inclusive, and the exception is thrown at pc offset 12. The exception caught by the entry is of class Ball, and the exception thrown is of class Ball. Given this perfect match, the Java virtual machine pushes the thrown exception object onto the stack, and continues execution at pc offset 19. The catch clause merely resets int i to 0, and the loop starts over.

To drive the simulation, just press the "Step" button. Each press of the "Step" button will cause the Java virtual machine to execute one bytecode instruction. To start the simulation over, press the "Reset" button. To cause the Java virtual machine to repeatedly execute bytecodes with no further coaxing on your part, press the "Run" button. The Java virtual machine will then execute the bytecodes until the "Stop" button is pressed. The text area at the bottom of the applet describes the next instruction to be executed. Happy clicking.


About the author
Bill Venners has been writing software professionally for 12 years. Based in Silicon Valley, he provides software consulting and training services under the name Artima Software Company. Over the years he has developed software for the consumer electronics, education, semiconductor, and life insurance industries. He has programmed in many languages on many platforms: assembly language on various microprocessors, C on Unix, C++ on Windows, Java on the Web. He is author of the book: Inside the Java Virtual Machine, published by McGraw-Hill. You can reach him at

This article was first published under the name How the Java virtual machine handles exceptions in JavaWorld, a division of Web Publishing, Inc., January 1997.

<<  Page 2 of 2

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use