ExceptionHandling with MethodOverriding in Java

There are many rules if we talk about methodoverriding with exception handling. The Rules are as follows:

  1. If the superclass method does not declare an exception

    If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.

  2. If the superclass method declares an exception

    If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

If the superclass method does not declare an exception

1) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.

Flash Soon


Output:


2) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.

Flash Soon


Output:



If the superclass method declares an exception

1) Rule: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares parent exception

Flash Soon


Example in case subclass overridden method declares same exception

Flash Soon


Example in case subclass overridden method declares subclass exception

Flash Soon


Example in case subclass overridden method declares no exception

Flash Soon



Copyright ©2017 Design& Development