The class Error
and its standard subclasses are exceptions from which ordinary
programs are not ordinarily expected to recover. The class Error
is a separate
subclass of Throwable
, distinct from Exception
in the class hierarchy, to allow
programs to use the idiom:
} catch (Exception e) {
to catch all exceptions from which recovery may be possible without catching errors from which recovery is typically not possible.
Package java.lang
defines all the error classes described here. These classes, like all other classes in package java.lang
, are implicitly imported and therefore may be referred to by their simple names.
A Java Virtual Machine throws an object that is an instance of a subclass of
LinkageError
when a loading, linkage, preparation, verification or initialization
error occurs:
ClassFormatError
, ClassCircularityError
, and NoClassDefFoundError
are described there.
IllegalAccessError
, InstantiationError
, NoSuchFieldError
, and NoSuchMethodError
, all of which are subclasses of IncompatibleClassChangeError
, and, also, UnsatisfiedLinkError
.
VerifyError
is described there.
AbstractMethodError
.
ExceptionInInitializerError
if execution of a static initializer or of an initializer for a static
field results in an exception that is not an Error
or a subclass of Error
.
A Java Virtual Machine throws an object that is an instance of a subclass of the
class VirtualMachineError
when an internal error or resource limitation prevents it from implementing the semantics of the Java Language. This language
specification and the Java Virtual Machine Specification define the following virtual machine errors:
InternalError
: An internal error has occurred in a Java Virtual Machine, because of a fault in the software implementing the virtual machine, a fault in the underlying host system software, or a fault in the hardware. This error is delivered asynchronously when it is detected, and may occur at any point in a Java program.
OutOfMemoryError
: A Java Virtual Machine has run out of either virtual or physical memory, and the automatic storage manager wasn't able to reclaim enough memory to satisfy an object creation request.
StackOverflowError
: A Java Virtual Machine has run out of stack space for a thread, typically because the thread is doing an unbounded number of recursive invocations due to a fault in the executing program.
UnknownError
: An exception or error has occurred but, for some reason, a Java Virtual Machine is unable to report the actual exception or error.
A sophisticated Java program may be designed to handle OutOfMemoryError
and
attempt to recover from it, perhaps by carefully dropping references to objects.
We are exploring enhancements to Java to simplify handling of out-of-memory conditions. One possibility would be to support automatic suspension of a thread which encounters an OutOfMemoryError
and allow another thread to handle the error
situation. Such a technique might also permit a Java program to recover from a StackOverflowError
if this overflow does not result from a nonterminating recursion. Suggestions for other approaches are welcomed.
No rule is so general, which admits not some exception.
Robert Burton (1576-1640)