The Play Ball! Applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes. This applet accompanies Chapter 17, "Exceptions," of Inside the Java 2 Virtual Machine. The bytecode sequence in the simulation was generated by // On CD-ROM in file except/ex2/Ball.java class Ball extends Exception { } // On CD-ROM in file except/ex2/Pitcher.java class Pitcher { private static Ball ball = new Ball(); static void playBall() { int i = 0; for (;;) { try { if (i % 4 == 3) { throw ball; } ++i; } catch (Ball b) { i = 0; } } } } The bytecodes generated by // The main bytecode sequence for playBall(): 0 iconst_0 // Push constant 0 1 istore_0 // Pop into local var 0: int i = 0; // The try block starts here (see the // 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 #6, which is the Ball exception eager // to be thrown 9 getstatic #6 The 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 To drive the Play Ball! simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking. Click here to view a page of links to the source code of the Play Ball! applet. |
Copyright © 1996-1999 Bill Venners. All Rights Reserved. |