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

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,

 

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

 

Rules for Exception handling w.r.t MethodOverriding

  • 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

 

Conclusion:

  • When parent-class method declares no exception, then child-class overriding-method can declare,
    1. No exception or
    2. Any number of unchecked exception
    3. but strictly no checked exception
  • When parent-class method declares unchecked exception, then child-class overriding-method can declare,
    1. No exception or
    2. Any number of unchecked exception
    3. but strictly no checked exception
  • When parent-class method declares checked exception, then child-class overriding-method can declare,
    1. No exception or
    2. Same checked exception or
    3. Sub-type of checked exception or
    4. any number of unchecked exception
  • All above conclusion hold true, even if combination of both checked & unchecked exception is declared in parent-class’ method

 

References:

 

Happy Coding !!
Happy Learning !!

Exception propagation in Java
throw v/s throws in Java