Java – Rules for Exception handling w.r.t Method Overriding

In this article, we will discuss the rules for exception handling when method is overridden in the child-class from parent-class

Before discussing rules for exception handling one should have good knowledge about,

1. Different cases for Method Overriding:

We will consider different cases for discussing rules for exception handling w.r.t method overriding

  1. If parent-class method doesn’t declare any exception
  2. If parent-class method declares unchecked exception
  3. If parent-class method declares checked exception
  4. If parent-class method declares both checked & unchecked exceptions

2. Rules for Exception handling w.r.t Method Overriding:

  • Let us see each case with an example

Rule 1: If parent-class method doesn’t declare any exception

  1. Then child-class overriding-method can declare any type of unchecked exception

    Note: this is the only possibility

  2. If child-class overriding-method declares checked-exception, then compiler throws compile-time error stating

    CTE – “Exception IOException is not compatible with throws clause in ParentClass.testMethod()

  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class

    This is very much same as that of overridden-method of parent-class (exactly same method-signature)

Rule 2: If parent-class method declares uncheckedexception

  1. Then child-class overriding-method can declare any type of unchecked exception

    Not necessarily same exception as that of parent-class’ method (only for unchecked exception)

  2. If child-class overriding-method declares any checked-exception, then compiler throws compile-time error stating

    CTE – “Exception IOException is not compatible with throws clause in ParentClass.testMethod()

  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class

Rule 3: If parent-class method declares checked exception

  1. Then child-class overriding-method can declare any type of unchecked exception

  2. Then child-class overriding-method can declare same type of checked exception or one of its sub-class or no exception

    OR, sub-type of declared checked exception

  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class

Rule 4: If parent-class method declares combination of both checked & unchecked exceptions

  1. Then child-class overriding-method can declare any type of unchecked exception

  2. Then child-class overriding-method can declare same type of checked-exception or one of its sub-class or no exception

  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class

3. Conclusion:

  • When parent-class method declares no exception, then child-class overriding-method can declare,
    • No exception
    • Any number of unchecked exception
    • But checked exception allowed
  • When parent-class method declares unchecked exception, then child-class overriding-method can declare,
    • No exception
    • Any number of unchecked exception
    • But checked exception allowed
  • When parent-class method declares checked exception, then child-class overriding-method can declare,
    • No exception
    • Same checked exception
    • Sub-type of checked exception
    • Any number of unchecked exception
  • All above conclusion hold true, even if combination of both checked & unchecked exception is declared in parent-class‘ method

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Exception propagation
Java - throw v/s throws