CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Exceptions

Exceptions handling
Introduction
Exception hierarchy

Exception hierarchy

  • An exception is an object of the Exception class or any of its subclasses. It represents a condition that prevents a method from successfully completing its function.

  • Checked exceptions are checked by the compiler. So you must supply code that handles any checked exceptions or your program won't compile.

  • Unchecked exceptions are not checked by the compiler, but you can still write code that handles them. If you don't, any unchecked exceptions will cause your program to terminate.
  • In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. For example, a NullPointerException is thrown if you access an object through a reference variable before an object is assigned to it; an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are the logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions.

  • The Error class, like the Exception class, is derived from the Throwable class. However, the Error class identifies internal errors that are rare and can't usually be recovered from.

Common checked exceptions

ClassNotFoundException
IOException
    EOFException
    FileNotFoundException
NoSuchMethodException

Common unchecked exceptions

ArithmeticException
IllegalArgumentException
    NumberFormatException
IndexOutOfBoundsException
    ArrayIndexOutOfBoundsException
    StringIndexOutOfBoundsException
NullPointerException
Previous | Java's Mechanism of Exception Handling | Exception hierarchy | Exceptions propagation | Next