Exception Handling with Method Overriding in Java

Exception Handling with Method Overriding in Java

When a subclass overrides a method from its parent class, specific rules regarding exception handling must be followed. Let’s examine these rules along with illustrative examples.

When the Superclass Method Does Not Declare an Exception

Rule 1: If the method in the superclass does not declare any exception, the overridden method in the subclass cannot declare checked exceptions.

Example:

class Parent {
    // Method in the superclass does not declare exceptions
    void msg() {
        System.out.println("Parent method");
    }
}

public class Child extends Parent {
    // Overriding the method in the child class
    // This will result in a compile-time error if a checked exception is declared
    void msg() throws IOException { // Error: cannot throw a checked exception
        System.out.println("Child method");
    }

    public static void main(String args[]) {
        Parent p = new Child();
       try {
             p.msg(); // This line won't execute due to the compile-time error
           } catch (Exception e) {
             e.printStackTrace();
        }  
    }
}        

Output: The code will produce a compile-time error because the overridden method in Child tries to declare a checked exception (IOException), which is not permitted as the superclass method does not declare any exceptions.


Rule 2: If the method in the superclass does not declare any exception, the overridden method in the subclass cannot declare checked exceptions, although it can declare unchecked exceptions.

Example :

class Parent{    
 // Method in the superclass does not declare exceptions
  void msg() {  
    System.out.println("Parent method");  
  }    
}    
    
public class Child extends Parent{    
 // Overriding the method in the child class
  void msg() throws ArithmeticException {    
    System.out.println("Child method");    
  }    
  
  public static void main(String args[]) {    
     Parent p = new Child();    
      try {
            p.msg(); // This will run and print "Child method"
        } catch (Exception e) {
            e.printStackTrace();
        }   
  }    
}           

Output: The output of the code will be: Child method. As ArithmeticException is a unchecked exceptions.


When the Superclass Method Declares an Exception

Rule 1: If the superclass method declares an exception, the overridden method in the subclass can declare the same exception, a subclass exception, or no exception, but cannot declare a parent exception.


Example 1: Declaring the Same Exception

class Parent {
    // Method in the superclass declares an exception
    void msg() throws Exception {
        System.out.println("Parent method");
    }
}

public class Child extends Parent {
    // Overriding the method and declaring the same exception
    void msg() throws Exception {
        System.out.println("Child method");
    }

    public static void main(String args[]) {
        Parent p = new TestExceptionChild1();
        try {
            p.msg(); // This will run and print "Child method"
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}        

Output: The output of the code will be: Child method


Example 2: Declaring the Subclass Exception

class Parent {
    // Method in the superclass declares an exception
    void msg() throws Exception {
        System.out.println("Parent method");
    }
}

class Child extends Parent {
    // Overriding the method and declaring a subclass exception
    void msg() throws ArithmeticException {
        System.out.println("Child method");
    }

    public static void main(String args[]) {
        Parent p = new Child();
        try {
            p.msg(); // This will run and print "Child method"
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}        

Output: The output of the code will be: Child method


Example 3: Declaring No Exception

class Parent {
    // Method in the superclass declares an exception
    void msg() throws Exception {
        System.out.println("Parent method");
    }
}

class Child extends Parent {
    // Overriding the method and not declaring any exceptions
    void msg() {
        System.out.println("Child method");
    }

    public static void main(String args[]) {
        Parent p = new TestExceptionChild3();
        try {
            p.msg(); // This will run and print "Child method"
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}        

Output: The output of the code will be: Child method


Conclusion

To summarize, grasping how exception handling operates during method overriding in Java is essential for writing effective software. The rules are straightforward:

  • If the superclass method does not declare an exception, the subclass cannot declare checked exceptions but may declare unchecked exceptions.
  • If the superclass method declares an exception, the subclass can declare the same exception, a subclass exception, or no exception, but cannot declare a parent exception.

Following these rules contributes to creating robust and maintainable Java applications. Feel free to reach out with questions or to share your experiences with exception handling in Java!

Diwakar Singh

SWE Intern at Novoinvent

1 个月

Insightful

要查看或添加评论,请登录

Amit Rautela的更多文章

  • CanActivate vs. CanLoad in Angular

    CanActivate vs. CanLoad in Angular

    What Are They? CanActivate: This guard checks if a user can enter a specific route (like a page) in your app. CanLoad:…

    1 条评论

社区洞察

其他会员也浏览了