4 Rules for Overriding Methods Exception Handling

Before discussing about 4 rules to handle exception in overriding method. You should understand concepts of

Exception handling in override method these are 4 rules where you have to handle exceptions for child class according to given parent class scenarios:

4 Rules for Exception Handling in Overring Method
4 Rules for Exception Handling in Overring Method

Note : When you face any Java interview and test at least one or two questions surely will ask based on these cases to check you Exception Handling concepts and practical knowledge.

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

  1. Child class overriding method can declare no exception in overriding method.

    Overriding Method No Exception throws
    Overriding Method No Exception throws
  2. Child class overriding method can declare unchecked exception in overriding method.

    Overriding Method Throws Unchecked Exception
    Overriding Method Throws Unchecked Exception
  3. Child class overriding method can declare strictly no checked exception in overriding method.

    Override method throws no checked exception
    Override method throws no checked exception

Case 2 : If parent-class method declares unchecked exception

  1. Child class overriding method can declare no exception in overriding method.

    Parent Uncheck child no excepton
    Parent Class Unchecked Exception Child Class No Exception
  2. Child class overriding method can declare any number of unchecked exception in overriding method.

    Parent Unchecked exception Child any Number Unchecked exception
    Parent Class method UncheckedException and Child Class overriding method unlimited Unchecked Exception
  3. Child class overriding method can declare strictly no checked exception in overriding method.

    Parent Class Unchecked Exception and Child class Checked Exception
    Parent Class Unchecked Exception and Child class overriding method Checked Exception not allow.

Case 3 : If parent-class method declares checked exception

  1. Child class overriding method can declare no exception in overriding method.

    Parent Class Method Checked Exception Child Class No Exception
    Parent Class Method Checked Exception Child Class Overriding Method No Exception
  2. Child class overriding method can declare same checked exception in overriding method.

    Parent Class Checked Exception Child Class same Checked Exception
    Parent Class method Checked Exception Child Class overriding method same Checked Exception
  3. Child class overriding method can declare sub-type of checked exception in overriding method.Parent class checked exception and child class sub class checked exception
  4. Child class overriding method can declare any number of unchecked exception in overriding method.Parent class checked exception child class any number unchecked exception

Case 4 : If parent-class method declares both checked & unchecked exceptions

  1. Child class overriding method can declare no exception in overriding method.

    Parent class checked and uncheked exception child no exception
    Parent class checked and un-cheked exception child class overriding method no exception
  2. Child class overriding method can declare same checked exception in overriding method.

    Parent class having checked and uncheck exception both and child class checked exception
    Parent class having checked and uncheck exception both and child class overriding method having same checked exception
  3. Child class overriding method can declare sub-type of checked exception in overriding method.

    Parent class having checked and uncheked exception child class having sub class of cheked exception
    Parent class having checked and uncheked exception child class overriding method having sub class of checked exception
  4. Child class overriding method can declare any number of unchecked exception in overriding method.

    Parent class having cheked and unchecked exception and child class having unlimited unchecked exception
    Parent class having chceked and unchecked exception and child class overriding method having unlimited unchecked exception

Conclusions

I have covered all scenarios for exception handling in overriding method:

  • If parent-class method doesn’t declare any exception, then child-class overriding-method can declare,
    • 1. No exception or
    • 2. Any number of unchecked exception
    • 3. but strictly no checked exception
  • If parent-class method  declare 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
  • If parent-class method  declare 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

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s