Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 4
A Java programmer can throw an exception with a throw statement such as the one in the catch (ArithmeticException) clause of remainder, where a DivideByZeroException is created and thrown. The bytecode that does the throwing is shown in the following table:
| Opcode | Operand(s) | Description |
|---|---|---|
| athrow | (none) | pops Throwable object reference, throws the exception |
The athrow instruction pops the top word from the stack and expects it to be a reference to an object that is a subclass of Throwable (or Throwable itself). The exception thrown is of the type defined by the popped object reference.
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;
}
++i;
}
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.