The Exceptions attribute is a variable-length attribute in the attributes table of a method_info structure (§4.6). The Exceptions attribute indicates which checked exceptions a method may throw.
There may be at most one Exceptions attribute in the attributes table of a method_info structure.
The Exceptions attribute has the following format:
Exceptions_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_exceptions;
u2 exception_index_table[number_of_exceptions];
}
The items of the Exceptions_attribute structure are as follows:
-
attribute_name_index
The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be theCONSTANT_Utf8_infostructure (§4.4.7) representing the string "Exceptions". -
attribute_length
The value of the
attribute_lengthitem indicates the attribute length, excluding the initial six bytes. -
number_of_exceptions
The value of the
number_of_exceptionsitem indicates the number of entries in theexception_index_table. -
exception_index_table[]
Each value in the
exception_index_tablearray must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_infostructure (§4.4.1) representing a class type that this method is declared to throw.
A method should throw an exception only if at least one of the following three criteria is met:
- The exception is an instance of
RuntimeExceptionor one of its subclasses. - The exception is an instance of
Erroror one of its subclasses. - The exception is an instance of one of the exception classes specified in the
exception_index_tablejust described, or one of their subclasses.
These requirements are not enforced in the Java Virtual Machine; they are enforced only at compile time.