Posted on Leave a comment

Overriding the throws Clause – Exception Handling

Overriding the throws Clause

A subclass can override a method defined in its superclass by providing a new implementation (ยง5.1, p. 196). What happens when a superclass method with a list of exceptions in its throws clause is overridden in a subclass? The method declaration in the subclass need not specify a throws clause if it does not throw any checked exceptions, and if it does, it can specify only checked exception classes that are already in the throws clause of the superclass method, or that are subclasses of the checked exceptions in the throws clause of the superclass method. As a consequence, an overriding method can have more number of exceptions, but it cannot allow broader checked exceptions in its throws clause than the superclass method does. Allowing broader checked exceptions in the overriding method would create problems for clients who already deal with the exceptions specified in the superclass method. Such clients would be ill prepared if an object of the subclass threw a checked exception they were not prepared for. However, there are no restrictions on specifying unchecked exceptions in the throws clause of the overriding method. The preceding discussion also applies to overriding methods from an interface that a class implements.

In the code below, the method compute() at (1) in superclass A is overridden correctly at (2) in subclass B. The throws clause of the method at (2) in subclass B specifies only one checked exception (ThirdException) from the throws clause at (1) and adds the more specific subclass exception (SubFirstException) of the superclass exception (FirstException) that is specified in the throws clause at (1). An unchecked exception (NumberFormatException) is also specified in the throws clause at (2). The checked exceptions in the throws clause at (2) are covered by the checked exceptions specified in the throws clause at (1). Unchecked exceptions are inconsequential in this regard. The subclass C does not override the compute() method from class A correctly, as the throws clause at (3) specifies an exception (FourthException) that the overridden method at (1) in class A cannot handle.

Click here to view code image

// New exception classes:
class FirstException    extends Exception { }
class SecondException   extends Exception { }
class ThirdException    extends Exception { }
class FourthException   extends Exception { }
class SubFirstException extends FirstException { }
// Superclass
class A {
  protected void compute()
      throws FirstException, SecondException, ThirdException { /* … */ }  // (1)
}
// Subclass
class B extends A {
  @Override
  protected void compute()
      throws ThirdException, SubFirstException, NumberFormatException {     // (2)
    /* … */
  }
}
//Subclass
class C extends A {
  @Override
  protected void compute()                            // Compile-time error at (3)
      throws FirstException, ThirdException, FourthException { /* … */ }  // (3)
}

Usage of checked and unchecked exceptions in different contexts is compared in Table 7.1.

Table 7.1 Comparing Checked and Unchecked Exceptions

Leave a Reply

Your email address will not be published. Required fields are marked *