Java Exceptions  «Prev  Next»

Lesson 8Java Exception Subclasses:
ObjectiveIdentify Java's Exception Subclasses and State the Difference between Checked and Unchecked Exceptions

Identify Java's Exception Subclasses

State the Difference between Checked and Unchecked Exceptions

Java defines a class called Exception, which represents generic unexpected events.
Subclasses define specific unexpected events. There are two general categories of exceptions:
  1. checked and
  2. unchecked.
Unchecked exceptions descend from RuntimeException and Error, which are subclasses of Exception. You should leave unchecked exceptions to Java.
That is, you should always throw checked exceptions, never subclasses of RuntimeException or Error. You can look through the APIs for the different types of checked and unchecked exceptions that Java defines.

The difference between a checked and unchecked exception is that you do not have to wrap the call to a method that might throw an unchecked exception in a try/catch block, while you must do so for a checked exception. But unchecked exceptions will halt your thread in the same manner as checked exceptions if you do not catch them. Here is a listing of some of the more common exceptions in Java and where they might occur:

When to throw an exception

A quick way to determine who should throw an exception is to see if the exception extends java.lang.Error.
Errors are always thrown only by the JVM. Generally, RuntimeExceptions are also thrown by the JVM. However, it is acceptable for an application code to throw a RuntimeException if it makes sense for the application to throw a RuntimeException in a given situation.

Common Exception Classes

You should know about the following common exception classes:
  1. IndexOutOfBoundsException extends RuntimeException: Usually thrown by the JVM. Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range. Applications can subclass this class to indicate similar exceptions.
  2. ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException both extend IndexOutOfBoundsException.
  3. IllegalArgumentException extends RuntimeException: Thrown to indicate that a method has been passed an illegal or inappropriate argument. Usually thrown by the application.
  4. IllegalStateException extends RuntimeException: Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation. Usually thrown by the application.
  5. ClassCastException extends RuntimeException: Usually thrown by the JVM. Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
    Object x = new Integer(0);      
    System.out.println((String)x);   
    
  6. NullPointerException extends RuntimeException: Usually thrown by the JVM. NullPointerException is thrown when an application attempts to use null in a case where an object is required.
    These include:
    1. Calling the instance method of a null object.
    2. Accessing or modifying the field of a null object.
    3. Taking the length of null as if it were an array.
    4. Accessing or modifying the slots of null as if it were an array.
    Throwing null as if it were a Throwable value.
    Applications should throw instances of this class to indicate other illegal uses of the null object.
  7. SecurityException extends RuntimeException: Usually thrown by the JVM. It is thrown by the security manager upon security violation. For example, when a Java program runs in a sandbox (such as an applet) and it tries to use prohibited APIs such as File I/O, the security manager throws this exception.