|
CHAPTER 11: Exceptions |
|
 Previous |
 Java Language |
 Index |
 Next |
11.5 The Exception Hierarchy
11.5.1 The Classes Exception and RuntimeException
, 11.5.2 The Class Error
The possible exceptions in a Java program are organized in a hierarchy of classes, rooted at class Throwable
(S11.5, S20.22), a direct subclass of Object
. The classes Exception
and Error
are direct subclasses of Throwable
. The class RuntimeException
is a direct subclass of Exception
.
The exception classes declared by the standard packages java.lang, java.util, java.io and java.net are called the standard exception classes.
Java programs can use the pre-existing exception classes in throw
statements, or define additional exception classes, as subclasses of Throwable
or of any of its subclasses, as appropriate. To take advantage of Java's compile-time checking for exception handlers, it is typical to define most new exception classes as checked exception classes, specifically as subclasses of Exception
that are not subclasses of RuntimeException
.
The class Exception
is the superclass of all the exceptions that ordinary programs may wish to recover from.
The class RuntimeException
is a subclass of class Exception
. The subclasses of RuntimeException
are unchecked exception classes.
Package java.lang
defines the following standard unchecked runtime exceptions, which, like all other classes in package java.lang
, are implicitly imported and therefore may be referred to by their simple names:
- ArithmeticException
: An exceptional arithmetic situation has arisen, such as an integer division (S15.16.2) operation with a zero divisor.
- ArrayStoreException
: An attempt has been made to store into an array component a value whose class is not assignment compatible with the component type of the array (S10.10, S15.25.1).
- ClassCastException
: An attempt has been made to cast (S5.4, S15.15) a reference to an object to an inappropriate type.
- IllegalArgumentException
: A method was passed an invalid or inappropriate argument or invoked on an inappropriate object. Subclasses of this class include:
- IllegalThreadStateException:
A thread was not in an appropriate state for a requested operation.
- NumberFormatException
: An attempt was made to convert a String
to a value of a numeric type, but the String
did not have an appropriate format.
- IllegalMonitorStateException
: A thread has attempted to wait on (S20.1.6, S20.1.7, S20.1.8) or notify (S20.1.9, S20.1.10) other threads waiting on an object that it has not locked.
- IndexOutOfBoundsException
: Either an index of some sort (such as to an array, a string, or a vector) or a subrange, specified either by two index values or by an index and a length, was out of range.
- NegativeArraySizeException
: An attempt was made to create an array with a negative length (S15.9).
- NullPointerException
: An attempt was made to use a null reference in a case where an object reference was required.
- SecurityException
: A security violation was detected (S20.17).
Package java.util
defines the following additional standard unchecked runtime exceptions:
- java.util.EmptyStackException
: An attempt was made to access an element of an empty stack.
- java.util.NoSuchElementException
: An attempt was made to access an element of an empty vector.
The standard subclasses of Exception
other than RuntimeException
are all checked exception classes.
Package java.lang
defines the following standard exceptions, which, like all other classes in package java.lang
, are implicitly imported and therefore may be referred to by their simple names:
- ClassNotFoundException
: A class or interface with a specified name could not be found (S20.3.8).
- CloneNotSupportedException
: The clone
method (S20.1.5) of class Object
has been invoked to clone an object, but the class of that object does not implement the Cloneable
interface.
- IllegalAccessException
: An attempt has been made to load a class using a string giving its fully qualified name, but the currently executing method does not have access to the definition of the specified class because the class is not public
and is in another package.
- InstantiationException
: An attempt was made to create an instance of a class using the newInstance
method in class Class
, but the specified class object cannot be instantiated because it is an interface, is abstract
, or is an array.
- InterruptedException
: The current thread was waiting, and another thread has interrupted the current thread, using the interrupt
method of class Thread
(S20.20.31).
Package java.io
defines the following additional standard exceptions:
- java.io.IOException
: A requested I/O operation could not be completed normally. Subclasses of this class include:
- java.io.EOFException
: End of file has been encountered before normal completion of an input operation.
- java.io.FileNotFoundException
: A file with the name specified by a file name string or path was not found within the file system.
- java.io.InterruptedIOException
: The current thread was waiting for completion of an I/O operation, and another thread has interrupted the current thread, using the interrupt
method of class Thread
(S20.20.31).
- java.io.UTFDataFormatException
: A requested conversion of a string to or from Java modified UTF-8 format could not be completed (S22.1.15, S22.2.14) because the string was too long or because the purported UTF-8 data was not the result of encoding a Unicode string into UTF-8.
The standard package java.net
defines the following additional subclasses of java.io.IOException
:
u java.net.MalformedURLException
: A string that was provided as a URL, or as part of a URL, had an inappropriate format or specified an unknown protocol.
- java.net.ProtocolException
: Some aspect of a network protocol was not correctly carried out.
- java.net.SocketException
: An operation involving a socket could not be completed normally.
- java.net.UnknownHostException
: The name of a network host could not be resolved to a network address.
- java.net.UnknownServiceException
: The network connection cannot support the requested service.
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:
- The loading process is described in S12.2. The errors ClassFormatError
, ClassCircularityError
, and NoClassDefFoundError
are described there.
- The linking process is described in S12.3. The linking errors are described there. These errors include IllegalAccessError
, InstantiationError
, NoSuchFieldError
, and NoSuchMethodError
, all of which are subclasses of IncompatibleClassChangeError
, and, also, UnsatisfiedLinkError
.
- The class verification process is described in S12.3.1. The verification failure error VerifyError
is described there.
- The class preparation process is described in S12.3.2. The preparation error described there is AbstractMethodError
.
- The class initialization process is described in S12.4. A virtual machine will throw the error 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.
 | © 1996 Sun Microsystems, Inc. All rights reserved. |