CHAPTER 11: Exceptions Previous
Previous
Java Language
Java Language
Index
Index
Next
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 .


11.5.1 The Classes Exception and RuntimeException

The class Exception is the superclass of all the exceptions that ordinary programs may wish to recover from.

11.5.1.1 Standard Runtime Exceptions

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:

Package java.util defines the following additional standard unchecked runtime exceptions:


11.5.1.2 Standard Checked Exceptions

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:

Package java.io defines the following additional standard exceptions:

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.


11.5.2 The Class Error

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.

11.5.2.1 Loading and Linkage Errors

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:


11.5.2.2 Virtual Machine Errors

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:

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.

Top© 1996 Sun Microsystems, Inc. All rights reserved.