Classes that inherit from the Error class are for exceptions that are thrown when

Java provides a mechanism to handle exceptions. To learn about exception handling, you can refer to exceptions in java. In this article, we discuss exception handling with constructors when inheritance is involved. In Java, if the constructor of the parent class throws any checked exception, then the child class constructor can throw the same exception or its parent classes. There is no problem if the parent class or child class constructor throws any unchecked exceptions. The child class constructor can throw anyunchecked exceptionwithout looking for a parent class constructor.

Understanding behavior of constructor calls

Whenever a method that throws some exception is called by another method, then the calling method is responsible for handling that exception (The calling method is the method that contains the actual call; the called method is the method being called). In case of constructors, the parent class constructor is called by the child class constructor. It means the child class constructor is responsible for handling the exception thrown by the parent class constructor. 

Now, for handling an exception there are two ways, one is to catch the exception and another is to throw it. But in the case of the constructor, we can’t handle it using the try-catch mechanism. The reason is that we enclose our code which can raise an exception in the try block and then catch it. The exception is raised due to a call to parent class constructor, like super(). It means if we want to handle the exception using try-catch is depicted in the below illustration.

Illustration 1

Child() {

    // Try- catch block 
    try 
    {
        super();
    } 
    
    catch (FileNotFoundException exc) 
    {
      // Handling exception(code)
    }
}

Actually, it is not correct as a call to super must be first statement in the child class constructor (refer super in java  as it can be perceived from below illustration as follows:

Illustration 2

Child() {
   super(); // either called explicitly or added by the compiler in case of default constructor
   try {
       // your code 
      }
      catch(FileNotFoundException exc) {
       // handling code;  
      }
  }

and hence the exception can’t be caught (as its not inside the try block) and we can’t handle it using try-catch mechanism. That’s why we need to throw the exception. The below code will compile fine which will appear as follows:

// parent class constructor throws FileNotFoundException 
Child() throws FileNotFoundException  {
  super(); // either called explicitly or added by the compiler in case of default constructor
  try {
      // your code
     }
     catch(FileNotFoundException exc) {
      // handling code;  
     }
 }

Different Use-cases:

  1. Parent class constructor does not throw any checked exception
  2. Parent class constructor throws a checked exception

Now let us discuss each case in detail alongside justifying via clean java programs.

Case 1: Parent class constructor does not throw any checked exception

If the parent class constructor does not throw any exception then the child class can throw any exception or throw nothing.

Example 1

Java

class Parent {

    Parent()

    {

        System.out.println("parent class constructor");

    }

}

public class Child extends Parent {

    Child()

    {

        System.out.println("child class constructor");

    }

    public static void main(String[] args)

    {

        Child child = new Child();

    }

}

Output

parent class constructor
child class constructor

Example 2 

Java

class Parent {

    Parent()

    {

        System.out.println("parent class constructor");

    }

}

public class Child extends Parent {

    Child() throws Exception

    {

        System.out.println(

            "child class constructor throwing Exception");

    }

    public static void main(String[] args) throws Exception

    {

        Child child = new Child();

    }

}

Output

parent class constructor
child class constructor throwing Exception

Case 2: Parent class constructor throws a checked exception

If the parent class constructor throws a checked exception, then the child class constructor can throw the same exception or its super-class exception. Now at this point, the child class constructors have to throw the exception.

Example 

Java

import java.io.*;

class Parent {

    Parent() throws FileNotFoundException

    {

        System.out.println(

            "parent class constructor throwing exception");

    }

}

class Child extends Parent {

    Child()

    {

        System.out.println("child class constructor");

    }

    public static void main(String[] args) throws Exception

    {

        Child child = new Child();

    }

}

Output 

error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    Child() {
            ^

In order to resolve the error we need to declare the exceptions to be thrown. These exception can be of same or parent class.

Example 1 

Java

import java.io.*;

class Parent {

    Parent() throws FileNotFoundException {

        System.out.println("parent class constructor throwing checked exception");

    }

}

public class Child extends Parent {

    Child() throws FileNotFoundException {

        System.out.println("child class constructor throwing same exception");

    }

    public static void main(String[] args) throws Exception {

        Child child = new Child();

    }

}

Output 

parent class constructor throwing checked exception
child class constructor throwing same exception

Example 2 

Java

package package1;

import java.io.*;

class Parent {

    Parent() throws FileNotFoundException

    {

        System.out.println(

            "parent class constructor throwing checked exception");

    }

}

public class Child extends Parent {

    Child() throws IOException

    {

        System.out.println(

            "child class constructor throwing super-class exception");

    }

    public static void main(String[] args) throws Exception

    {

        Child child = new Child();

    }

}

Output 

parent class constructor throwing checked exception
child class constructor throwing super-class exception

When an exception is thrown by a method that is executing?

8) When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.

What is the difference between exceptions that inherit from the Error class and exceptions that inherit from the exception class?

What is the difference between exceptions that inherit from the Error class and exceptions that inherit from the Exception class? Error class does critical errors, internal errors in JVM, or no memory. Exception class does exceptions apps can handle.

When you write a method that throws a checked exception you must?

If a method declares a checked exception (i.e., an exception other than Error or RuntimeException), you must invoke it in a try-catch block or declare to throw the exception in the calling method.

What is exception inheritance in Java?

By Dinesh Thakur. An exception handler designed to handle a specific type of object may be preempted by another handler whose exception type is a super-class of that exception object.