The exception hierarchy in Java

13 08 2010

Exceptions

All of the subclasses of Exception represent exceptional conditions that a normal Java program may want to handle. Many of the standard exceptions are also subclasses of RuntimeException. Runtime exceptions represent runtime conditions that can generally occur in any Java method, so a method is not required to declare that it throws any of the runtime exceptions. However, if a method can throw any of the other standard exceptions, it must declare them in its throws clause.

A Java program should try to handle all of the standard exception classes, since they represent routine abnormal conditions that should be anticipated and caught to prevent program termination.

Exceptions are a means of dealing with error conditions where in other languages, workaround techniques need to be used such as reserving a special return value to indicate "error".

The easiest way to understand exceptions is dive straight in and look at an example. This is what one if the constructors to the FileInputStream class looks like:

public FileInputStream(File file) throws FileNotFoundException;

What this signature is telling us is that the constructor to FileInputStream, instead of giving us the constructed object, can instead "throw" an error condition back to us represented by an object of type FileNotFoundException. Now, to call this constructor, we have to add a piece of code to deal with the error condition:

try {
FileInputStream fin = new FileInputStream(file);
// No error occurred: read data from fin
} catch (FileNotFoundException fnf) {
// Oh dear, error occurred opening file
displayErrorMessage(fnf.getMessage());
}
// Do next bit

If the constructor to FileInputStream completes normally, we’ll be returned a FileInputStream object as expected. Otherwise, if a FileNotFoundException is thrown, Java will jump to the section of code that catches the exception. Note that these are the only two possibilities:

  • The constructor to FileInputStream must return a constructed FileInputStream object, else throw an exception;
  • The code inside the catch block will be executed if and only if an exception occurs. Under normal execution, once the code inside the try block completes, Java skips past any related catch block (to where the comment "do next bit" is in the example above);
  • Similarly, the remaining lines inside the try block will be executed if and only if the constructor completes without error.

Note the following about exceptions:

  • A single catch block will be called when the given exception happens on any line inside the try block; we don’t have to repeatedly check for an error condition after every line of code, as in the typical C idiom;
  • An exception is an object, so it has properties, such as an error message that we can query;
  • Because an exception is an object, this allows for a hierarchy of exception types. As we’ll see later, FileNotFoundException is actually a subtype of an exception class called IOException.

We mentioned above Java exceptions are objects. Since they’re objects, different types of exceptions can be subclasses of one another, just as with other objects. In fact, we mentioned that FileNotFoundException is an extension of IOException. If we catch IOException, then by implication, we also catch FileNotFoundException.

From the base upwards, Java exception classes are organised into a hierarchy. There is a basic exception class called Exception as you might expect. But in fact, the base of the hierarchy starts not with Exception but with a class called Throwable, which is then subclassed into Exception and Error. Part of the hierarchy is illustrated in Figure 1.

The rationale behind the hierarchy is as follows:

  • Exception subclasses represent errors that a program can reasonably recover from. Except for RuntimeException and its subclasses (see below), they generally represent errors that a program will expect to occur in the normal course of duty: for example, network connection errors and filing system errors.
  • Error subclasses represent "serious" errors that a program generally shouldn’t expect to catch and recover from. These include conditions such as an expected class file being missing, or an OutOfMemoryError.
  • RuntimeException is a further subclass of Exception. RuntimeException and its subclasses are slightly different: they represent exceptions that a program shouldn’t generally expect to occur, but could potentially recover from. They represent what are likely to be programming errors rather than errors due to invalid user input or a badly configured environment.
ExceptionHierarchy.png
Figure 1: The base of the Java exception hierarchy. Types in red, and their subclasses, are unchecked.

We’ll see that in reality, there are exceptions to these uses. For example, there are cases where the organisation of the JDK libraries means that we have to catch an exception that we essentially know will never occur. And there are some cases where we’ve "got nothing to loose" by trying to catch an Error, even though we may not actually be able to in practice.

Checked vs unchecked exceptions

We mentioned above that certain exceptions are generally indicative of a programming error rather than errors that we’d expect to occur in the normal course of events. (Whereas, it is reasonable for the programmer to expect that a networking or file system error will occur from time to time.) We also mentioned that subclasses of Error are not necessarily programming errors, but still errors that we wouldn’t really expect to occur in the normal course of events.

A feature built into the Java language is that Errors and RuntimeExceptions (and their subclasses– marked in red in Figure 1) are what are called unchecked exceptions:

  • unchecked exceptions can be thrown "at any time";
  • methods don’t explicitly have to declare that they can throw an unchecked exception;
  • callers don’t have to handle them explicitly.

Figure 2: Standard Java exception classes

[Graphic: Figure 9-1]

Figure 3: Standard Java error classes

[Graphic: Figure 9-2]

Exceptions

Runtime exceptions

The java.lang package defines the following standard runtime exception classes:

ArithmeticException

This exception is thrown to indicate an exceptional arithmetic condition, such as integer division by zero.

ArrayIndexOutOfBoundsException

This exception is thrown when an out-of-range index is detected by an array object. An out-of-range index occurs when the index is less than zero or greater than or equal to the size of the array.

ArrayStoreException

This exception is thrown when there is an attempt to store a value in an array element that is incompatible with the type of the array.

ClassCastException

This exception is thrown when there is an attempt to cast a reference to an object to an inappropriate type.

IllegalArgumentException

This exception is thrown to indicate that an illegal argument has been passed to a method.

IllegalMonitorStateException

This exception is thrown when an object’s wait(), notify(), or notifyAll() method is called from a thread that does not own the object’s monitor.

IllegalStateException

This exception is thrown to indicate that a method has been invoked when the run-time environment is in an inappropriate state for the requested operation. This exception is new in Java 1.1.

IllegalThreadStateException

This exception is thrown to indicate an attempt to perform an operation on a thread that is not legal for the thread’s current state, such as attempting to resume a dead thread.

IndexOutOfBoundsException

The appropriate subclass of this exception (i.e., ArrayIndexOutOfBoundsException or StringIndexOutOfBoundsException) is thrown when an array or string index is out of bounds.

NegativeArraySizeException

This exception is thrown in response to an attempt to create an array with a negative size.

NullPointerException

This exception is thrown when there is an attempt to access an object through a null object reference. This can occur when there is an attempt to access an instance variable or call a method through a null object or when there is an attempt to subscript an array with a null object.

NumberFormatException

This exception is thrown to indicate that an attempt to parse numeric information in a string has failed.

RuntimeException

The appropriate subclass of this exception is thrown in response to a runtime error detected at the virtual machine level. Because these exceptions are so common, methods that can throw objects that are instances of RuntimeException or one of its subclasses are not required to declare that fact in their throws clauses.

SecurityException

This exception is thrown in response to an attempt to perform an operation that violates the security policy implemented by the installed SecurityManager object.

StringIndexOutOfBoundsException

This exception is thrown when a String or StringBuffer object detects an out-of-range index. An out-of-range index occurs when the index is less than zero or greater than or equal to the length of the string.

Other exceptions

The java.lang package defines the following standard exception classes that are not runtime exceptions:

ClassNotFoundException

This exception is thrown to indicate that a class that is to be loaded cannot be found.

CloneNotSupportedException

This exception is thrown when the clone() method has been called for an object that does not implement the Cloneable interface and thus cannot be cloned.

Exception

The appropriate subclass of this exception is thrown in response to an error detected at the virtual machine level. If a program defines its own exception classes, they should be subclasses of the Exception class.

IllegalAccessException

This exception is thrown when a program tries to dynamically load a class (i.e., uses the forName() method of the Class class, or the findSystemClass() or the loadClass() method of the ClassLoader class) and the currently executing method does not have access to the specified class because it is in another package and not public. This exception is also thrown when a program tries to create an instance of a class (i.e., uses the newInstance() method of the Class class) that does not have a zero-argument constructor accessible to the caller.

InstantiationException

This exception is thrown in response to an attempt to instantiate an abstract class or an interface using the newInstance() method of the Class class.

InterruptedException

This exception is thrown to signal that a thread that is sleeping, waiting, or otherwise paused has been interrupted by another thread.

NoSuchFieldException

This exception is thrown when a specified variable cannot be found. This exception is new in Java 1.1.

NoSuchMethodException

This exception is thrown when a specified method cannot be found.

Errors

The subclasses of Error represent errors that are normally thrown by the class loader, the virtual machine, or other support code. Application-specific code should not normally throw any of these standard error classes. If a method does throw an Error class or any of its subclasses, the method is not required to declare that fact in its throws clause.

A Java program should not try to handle the standard error classes. Most of these error classes represent non-recoverable errors and as such, they cause the Java runtime system to print an error message and terminate program execution.

The java.lang package defines the following standard error classes:

AbstractMethodError

This error is thrown in response to an attempt to invoke an abstract method.

ClassCircularityError

This error is thrown when a circular reference among classes is detected during class initialization.

ClassFormatError

This error is thrown when an error is detected in the format of a file that contains a class definition.

Error

The appropriate subclass of this error is thrown when an unpredictable error, such as running out of memory, occurs. Because of the unpredictable nature of these errors, methods that can throw objects that are instances of Error or one of its subclasses are not required to declare that fact in their throws clauses.

ExceptionInInitializerError

This error is thrown when an unexpected exception is thrown in a static initializer. This error is new in Java 1.1.

IllegalAccessError

This error is thrown when a class attempts to access a field or call a method it does not have access to. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class changes after the class that references it was last compiled.

IncompatibleClassChangeError

This error or one of its subclasses is thrown when a class refers to another class in an incompatible way. This situation occurs when the current definition of the referenced class is incompatible with the definition of the class that was found when the referring class was compiled. For example, say class A refers to a method in class B. Then, after class A is compiled, the method is removed from class B. When class A is loaded, the run-time system discovers that the method in class B no longer exists and throws an error.

InstantiationError

This error is thrown in response to an attempt to instantiate an abstract class or an interface. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class is changed after the class that references it was last compiled.

InternalError

This error is thrown to signal an internal error within the virtual machine.

LinkageError

The appropriate subclass of this error is thrown when there is a problem resolving a reference to a class. Reasons for this may include a difficulty in finding the definition of the class or an incompatibility between the current definition and the expected definition of the class.

NoClassDefFoundError

This error is thrown when the definition of a class cannot be found.

NoSuchFieldError

This error is thrown in response to an attempt to reference an instance or class variable that is not defined in the current definition of a class. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class is changed after the class that references it was last compiled.

NoSuchMethodError

This error is thrown in response to an attempt to reference a method that is not defined in the current definition of a class. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class is changed after the class that references it was last compiled.

OutOfMemoryError

This error is thrown when an attempt to allocate memory fails.

StackOverflowError

This error is thrown when a stack overflow error occurs within the virtual machine.

ThreadDeath

This error is thrown by the stop() method of a Thread object to kill the thread. Catching ThreadDeath objects is not recommended. If it is necessary to catch a ThreadDeath object, it is important to re-throw the object so that it is possible to cleanly stop the catching thread.

UnknownError

This error is thrown when an error of unknown origins is detected in the run-time system.

UnsatisfiedLinkError

This error is thrown when the implementation of a native method cannot be found.

VerifyError

This error is thrown when the byte-code verifier detects that a class file, though well-formed, contains some sort of internal inconsistency or security problem.

VirtualMachineError

The appropriate subclass of this error is thrown to indicate that the Java virtual machine has encountered an error.

Thanks: http://docstore.mik.ua/orelly/java/langref/ch09_04.htm

http://www.javamex.com/tutorials/exceptions/exceptions_hierarchy.shtml

About these ads

Actions

Information

One response

20 12 2010
bilety-promowe

Hey there

Do you love travelling? So do I. What’s the most annoying thing in travelling? Costs and time you must spend moving from one point to another. It’s difficult to find solution with time but you can do something with prices. You can choose bilety promowe that are quite inexpensive and everybody can afford to buy them.

Greetings

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 1,456 other followers

%d bloggers like this: