next up previous
Next: Definition of new exceptions Up: Unit 10 Previous: The throws clause

Checked exceptions and runtime exceptions

Checked exceptions must be mentioned in the throws clause of all methods in which they could occur. Such exceptions are then propagated to the methods that call the one in which the exception occurs.

For example, if a method A declares to throw an exception of type MyException, all methods that call A must either declare themselves that they throw such an exception, or catch the exception (see later). Hence, the following code fragment is wrong, because the method B does not declare to throw exceptions of type MyException (and does not catch the exception that could be generated by A).

public static void A() throws MyException {
  ...
}

public static void B() {
  A();
}

Unchecked exceptions are objects of type (subclass of) RuntimeException. They represent exceptions that occur in the Java virtual machine during program execution. This class includes:

Note: It is not necessary that the subclasses of RuntimeException are mentioned in the throws clause (this is why they are celled unchecked).


next up previous
Next: Definition of new exceptions Up: Unit 10 Previous: The throws clause