elibraryportal Logo

Exception Handling with Method Overriding in Java

Some Rule are follows:

  • 1) If the superclass method does not declare an exception : subclass overridden method cannot declare the checked exception but it can declare unchecked exception.

    Example1

    Output

        Compile Time Error
    

    Example2

    OutPut

    Exception Handling with Method Overriding
  • 2) If the superclass method declares an exception: subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

    Example 1

    Output

        Compile Time Error
    

    Example 2

    Output

    Exception Handling with Method Overriding
  • 3) If Parent class method does not throws exception
  • 4) Sub class override method can not throw checked exception but can throw unchecked.