Exception handling in Inheritance

When we write any method in Java, we can use the throws keyword to throw any kind of exception that is not handled by the method itself. Any other method that calls this method should handle the thrown exception.

However in case of inheritance, there are certain rules that define what kind of exceptions can be thrown by an overriding method. We will discuss about these rules in this article.


Rule 1

When the parent class method (overridden method) doesn’t throw any exception, the child class method (overriding method) can throw any unchecked exception, however it can’t throw a checked exception.

Example 1:

The parent class method doesn’t throw any exception, the child class method throws unchecked exception.

class Parent{	
    public void testMethod(){
	System.out.println("Parent Method");
    }
}

class Child extends Parent{	
    public void testMethod() throws NullPointerException{
	System.out.println("Child Method");
    }
}

The above example will compile successfully. The child method throws NullPointerException which is an unchecked exception.

Example 2:

The parent class method doesn’t throw any exception, the child class method throws checked exception.

class Parent{	
    public void testMethod(){
	System.out.println("Parent Method");
    }
}

class Child extends Parent{	
    public void testMethod() throws IOException{
	System.out.println("Child Method");
    }
}

The above example would fail to compile. The child method throws IOException which is a checked exception. As it violates rule 1 it won’t compile.

Below is the error message:

- Exception IOException is not compatible with throws clause in Parent.testMethod()

Rule 2

When the parent class method (overridden method) throws a checked exception, the child class method (overriding method) can throw any unchecked exception or any checked exception that has a less scope than the exception thrown by the parent class method.

Example 1:

The parent method throws a checked exception, the child method doesn’t throw any exception.

class Parent{	
    public void testMethod() throws IOException{
	System.out.println("Parent Method");
    }
}

class Child extends Parent{	
    public void testMethod() {
	System.out.println("Child Method");
    }
}

The above example will compile successfully. When the parent class method throws an checked exception, the child class method may choose not to throw any exception.

Example 2:

The parent method throws a checked exception, the child method throws the same exception or any other checked exception that has a lower scope (child of the parent exception).

class Parent{
    
    public void testMethod1() throws IOException {
        System.out.println("Parent Method 1");
    }
    
    public void testMethod2() throws IOException {
        System.out.println("Parent Method 2");
    }
}
class Child extends Parent{
    
    @Override
    public void testMethod1() throws IOException {
        System.out.println("Child Method 1");
    }
    
    @Override
    public void testMethod2() throws FileNotFoundException{
        System.out.println("Parent Method 2");
    }
}

The above example will compile successfully. As rule 2 states when the parent class method throws a checked exception, the child class method can throw any other checked exception that has a lower scope than the exception thrown by the parent class method. Here FileNotFoundException is the child of IOException.

Example 3:

The parent method throws a checked exception, the child method throws another checked exception that has a higher scope.

class Parent{
    
    public void testMethod() throws FileNotFoundException {
        System.out.println("Parent Method");
    }
}
class Child extends Parent{
    
    @Override
    public void testMethod() throws IOException{
        System.out.println("Child Method");
    }
}

The above example fails to compile. When the parent class method throws a checked exception the child class method cannot throw a checked exception that has a higher scope than the exception thrown by the parent class method.

Example 4:

The parent method throws a checked exception, the child method throws another unchecked exception.

class Parent{
    
    public void testMethod() throws FileNotFoundException {
        System.out.println("Parent Method");
    }
}
class Child extends Parent{
    
    @Override
    public void testMethod() throws NullPointerException{
        System.out.println("Child Method");
    }
}

The above example compiles successfully. The child class method can throw any unchecked exception irrespective of what exception is thrown by the parent class method.


Rule 3

When the parent class method (overridden method) throws an unchecked exception, the child class method (overriding method) can throw any other unchecked exception.

Example 1:

The parent method throws an unchecked exception, the child method doesn’t throw any exception.

class Parent{
    
    public void testMethod() throws NullPointerException {
        System.out.println("Parent Method");
    }
}
class Child extends Parent{
    
    @Override
    public void testMethod() {
        System.out.println("Child Method");
    }
}

The above code compiles successfully. The child class method may choose not to throw any exception irrespective of the whether or not the parent class method throws any exception.

Example 2:

The parent method throws an unchecked exception, the child method throws a checked exception.

class Parent{
    
    public void testMethod() throws NullPointerException {
        System.out.println("Parent Method");
    }
}
class Child extends Parent{
    
    @Override
    public void testMethod() throws IOException {
        System.out.println("Child Method");
    }
}

The above code fails to compile. When the parent class method throws an unchecked exception, the child class method can’t throw a checked exception.

Example 3:

The parent method throws an unchecked exception, the child method throws another unchecked exception.

class Parent{
    
    public void testMethod() throws NullPointerException {
        System.out.println("Parent Method");
    }
}
class Child extends Parent{
    
    @Override
    public void testMethod() throws NumberFormatException {
        System.out.println("Child Method");
    }
}

This code compiles successfully. If the parent method throws an unchecked exception, the child method can throws any unchecked exception.

RELATED ARTICLES

2 ways to Create a Thread in Java

Threads in Java can be created in 2 different ways, extending the Thread class and implementing the Runnable interface. This article explains the two ways with suitable examples.

View Article

4 Different Ways of Creating an Object in Java

Most of us have created object using new operator in Java. Did you know the other ways to create objects in Java. This article talks about 4 different ways of creating objects in Java.

View Article

Using AutoCloseable with 'try-with-resources'

Java 7 added the java.lang.AutoCloseable interface that developers can implement in their custom classes for using them with try-with-resources.

View Article

Chained Exception

Chained exception is a feature introduced with JDK 1.4, which allow developers to associate an exception with another exception.

View Article

Shallow Cloning and Deep Cloning

Cloning an object can happen in two ways Deep and Shallow Cloning. In shallow cloning only object references are copied while in deep copy actual objects are copied to the newly cloned object.

View Article

Garbage Collection in Java

Java Garbage Collector is a boon to Java developers; it allows developers to program without worrying about memory management. This article describes the working of Java Garbage Collector in details.

View Article

Understanding static and final in Java

Java provides various modifiers which when used with variables, methods or classes, provides them with special properties. Static and final are are two such modifiers which are used frequently by Ja..

View Article

Understanding Object Cloning

Java provides the clone() method which can be used to create a copy an object. It creates a new memory location and copies the content of the object being cloned into the new location.

View Article

Try-with-resources Statement in Java 7

Java 7 introduces the new 'try-with-resources' statement which help developers to overcome the tedious task of closing the resources that have been used in the program.

View Article

Varargs: Variable Arguments to Methods

Java 5 introduced a new feature called varargs, which allows methods to accept variable number of arguments. They are useful in cases where the number of arguments to be passed to a method is unknown.

View Article