Java Method Overriding with Exception Handling

There are few things to remember when overriding a method with exception handling because method of super class may have declared exception.

There are some rules we have to follow while overriding method with exception handling. The Rules are as follows:

  1. superclass method does not declare an exception
    • subclass overridden method can declares no exception
    • subclass overridden method can declare unchecked exception(s)
    • subclass overridden method cannot declare checked exception(s)
  2. superclass method declares checked exception
    • subclass overridden method can declares no exception
    • subclass overridden method can declare same exception(s)
    • subclass overridden method can declare child exception(s)
    • subclass overridden method cannot declare parent exception(s)
  3. superclass method declares unchecked exception
    • subclass overridden method can declares no exception
    • subclass overridden method can declare same exception(s)
    • subclass overridden method can declare child exception(s)
    • subclass overridden method can declare parent exception(s)

Let us discuss these rules with examples. In the following example code itself we commented what is allowed and what is not allowed.

Example 01 : superclass method does not declare an exception

Example 02 : superclass method declares checked exception

Example 03 : superclass method declares unchecked exception

Scroll to Top