Open In App

Exception Handling with Method Overriding in Java

Last Updated : 02 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Exception handling with method overriding in Java refers to the rules and behavior that apply when a subclass overrides a method from its superclass and both methods involve exceptions. It ensures that the overridden method in the subclass does not declare broader or new checked exceptions than those declared by the superclass method, maintaining consistency and type safety during runtime execution

Rules for Exception Handling with Method Overriding

  • The subclass can throw the same or smaller exceptions as the superclass methods.
  • The subclass can choose not to throw any exceptions.
  • The subclass cannot throw new checked exceptions not in the parent method.

Example: Method Overriding with Unchecked Exception

Java
// Superclass without exception declaration
class SuperClass {
    void method() {
        System.out.println("SuperClass method executed");
    }
}

// Subclass declaring an unchecked exception
class SubClass extends SuperClass {
    @Override
    void method() throws ArithmeticException {
        System.out.println("SubClass method executed");
        throw new ArithmeticException("Exception in SubClass");
    }

    public static void main(String[] args) {
        SuperClass s = new SubClass();
        try {
            s.method();
        } catch (ArithmeticException e) {
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }
}

Output
SubClass method executed
Caught Exception: Exception in SubClass

Explanation: In this example, the SuperClass method does not declare any exceptions. The SubClass method overrides the method() and declares an unchecked exception i.e. the ArithmeticException. The main method demonstrates how the exception is thrown and caught in the SubClass.

Method Overriding with Checked Exception Rules

When exception handling is involved with method overriding, ambiguity occurs. The compiler gets confused as to which definition is to be followed. 

There are two types of problems associated with it which are as follows:

  • Problem 1: If the superclass does not declare an exception.
  • Problem 2: If the superclass declares an exception.

Let us discuss different cases under these problems and perceived their outputs.

Problem 1: Superclass Doesn't Declare an Exception

In this problem, two cases that will arise are as follows:

Case 1: If Superclass doesn't declare any exception and subclass declare checked exception.

Java
import java.io.*;

class SuperClass {

  // SuperClass doesn't declare any exception
  void method() {
    System.out.println("SuperClass");
  }
}

// SuperClass inherited by the SubClass
class SubClass extends SuperClass {

  // method() declaring Checked Exception IOException
  void method() throws IOException {

    // IOException is of type Checked Exception so the compiler will give Error
    System.out.println("SubClass");
  }

  public static void main(String args[]) {
    SuperClass s = new SubClass();
    s.method();
  }
}

Output:

Case 1 Output

Explanation: A subclass cannot introduce a new checked exception if the superclass method does not declare any.

Case 2: If Superclass doesn't declare any exception and Subclass declare unchecked exception.

Java
class SuperClass {

    // SuperClass doesn't declare any exception
    void method()
    {
        System.out.println("SuperClass");
    }
}

// SuperClass inherited by the SubClass
class SubClass extends SuperClass {

    // method() declaring Unchecked  Exception ArithmeticException
    void method() throws ArithmeticException
    {

        // ArithmeticException is of type Unchecked Exception
        System.out.println("SubClass");
    }

    public static void main(String args[])
    {
        SuperClass s = new SubClass();
        s.method();
    }
}

Output
SubClass

Explanation: ArithmeticException is unchecked, so compiler allows it.

Problem 2: Superclass Declares an Exception

If the Superclass declares an exception. In this problem 3 cases will arise as follows: 

Case 1: If Superclass declares an exception and Subclass declares exceptions other than the child exception of the Superclass declared exception.

Java
class SuperClass {
    void method() throws RuntimeException {
        System.out.println("SuperClass");
    }
}

// Subclass declares an unrelated exception
class SubClass extends SuperClass {
    @Override
    void method() throws Exception {
      
        // Exception is not a child of RuntimeException
        System.out.println("SubClass");
    }

    public static void main(String[] args) {
        SuperClass o = new SubClass();
        o.method();
    }
}

Output:

Output

Case 2: If Superclass declares an exception and Subclass declares a child exception of the Superclass declared exception.

Java
import java.io.*;

class SuperClass {

    // SuperClass declares an exception
    void method() throws RuntimeException
    {
        System.out.println("SuperClass");
    }
}

// SuperClass inherited by the SubClass
class SubClass extends SuperClass {

    // SubClass declaring a child exception of RuntimeException
    void method() throws ArithmeticException {

        System.out.println("SubClass");
    }

    public static void main(String args[])
    {
        SuperClass s = new SubClass();
        s.method();
    }
}

Output
SubClass

Case 3: If Superclass declares an exception and Subclass declares without exception.

Java
import java.io.*;

class SuperClass {

    // SuperClass declares an exception
    void method() throws IOException
    {
        System.out.println("SuperClass");
    }
}

// SuperClass inherited by the SubClass
class SubClass extends SuperClass {

    // SubClass declaring without exception
    void method()
    {
        System.out.println("SubClass");
    }

    public static void main(String args[])
    {
        SuperClass s = new SubClass();
    try {
        s.method();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

Output
SubClass
  • Case 1: The subclass cannot declare an exception unrelated to the superclass exception.
  • Case 2: The subclass can declare a child exception of the superclass exception.
  • Case 3: The subclass can choose to declare no exception.

Similar Reads