athrow

Throws an exception or error

Bytecode

Type Description
u1 athrow opcode = 0xBF (191)

Stack ..., objectref ==>

Description

Removes objectref (a reference to an object) from the operand stack, and 'throws' the exception represented by that object. objectref is an instance of Throwable or one of its subclasses. To throw an exception, the system searches for a handler for objectref's class in the exception table of the currently active method. If no handler is found, the current method's frame is discarded, its invoker's frame is reinstated, and the exception is immediately rethrown. This process is repeated until a handler is found or until there are no more procedures on the callstack (at which point, the current thread dies, typically printing out an error message).

If a handler is found, the operand stack of the active method is cleared, objectref is pushed on the operand stack of the current method, and execution continues at the first instruction of the handler.

The objectref must be of type reference and must refer to an object which is an instance of class Throwable or of a subclass of Throwable. It is popped from the operand stack. The objectref is then thrown by searching the current frame  for the most recent catch clause that catches the class of objectref or one of its superclasses.

If a catch clause is found, it contains the location of the code intended to handle this exception. The pc register is reset to that location, the operand stack of the current frame is cleared, objectref is pushed back onto the operand stack, and execution continues. If no appropriate clause is found in the current frame, that frame is popped, the frame of its invoker is reinstated, and the objectref is rethrown.

If no catch clause is found that handles this exception, the current thread exits.

Example

; Throw an IOException. This is equivalent to the Java code:
;
; throw new java.io.IOException();
;
new java/io/IOException     ; 1) create and initialize an IOException instance
dup
invokespecial java/io/IOException/<init>()V
athrow                                 ; 2) throw the IOException instance on the stack

Exception

NullPointerException - the objectref on the stack is null.

Notes

The operand stack diagram for the athrow instruction may be misleading: If a handler for this exception is found in the current method, the athrow instruction discards all the words on the operand stack, then pushes the thrown object onto the stack. However, if no handler is found in the current method and the exception is thrown farther up the method invocation chain, then the operand stack of the method (if any) that handles the exception is cleared and objectref is pushed onto that empty operand stack. All intervening stack frames from the method that threw the exception up to, but not including, the method that handles the exception are discarded.