English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Can a constructor throw an exception in Java?

AConstructionUsed to initialize objects at creation time. Grammatically, it is similar to a method. The difference is that the name of the constructor is the same as the class name, and it has no return type.

Constructors are called implicitly; they are automatically called when an instance is created.

Example

public class Example {
   public Example() {
      System.out.println("This is the constructor of the class example");
   }
   public static void main(String args[]) {
      Example obj = new Example();
   }
}

Output Result

This is the constructor of the class example

Constructor throws an exception

Yes, just like methods, you can throw exceptions from the constructor. However, if you do this, you need to catch them in the method that calls the constructor/Throw (handle) exceptions. If you do not compile, an error will be generated.

Example

In the following example, we have a class named Employee whose constructor throws IOException, and we instantiate the class without handling the exception. Therefore, if you compile the program, it will generate a compilation-time error.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Employee{
   private String name;
   private int age;
   File empFile;
   Employee(String name, int age, String empFile) throws IOException{
      this.name = name;
      this.age = age;
      this.empFile = new File(empFile);
      new FileWriter(empFile).write("Employee name is ",+name+"and age is "+age);
   }
   public void display(){
      System.out.println("Name: ",+name);
      System.out.println("Age: ",+age);
   }
}
public class ConstructorExample {
   public static void main(String args[]) {
      String filePath = "samplefile.txt";
      Employee emp = new Employee("Krishna", 25, filePath);
   }
}

Compilation-time error

ConstructorExample.java:23error: unreported exception IOException; must be caught or declared to be thrown
   Employee emp = new Employee("Krishna", 25, filePath);
                  ^
1 error

Example

To make the program work properly, please wrap the instantiation line in a try-In the catch block, or throw an exception.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Employee{
   private String name;
   private int age;
   File empFile;
   Employee(String name, int age, String empFile) throws IOException{
      this.name = name;
      this.age = age;
      this.empFile = new File(empFile);
      new FileWriter(empFile).write("Employee name is ",+name+"and age is "+age);
   }
   public void display(){
      System.out.println("Name: ",+name);
      System.out.println("Age: ",+age);
   }
}
public class ConstructorExample {
   public static void main(String args[]) {
      String filePath = "samplefile.txt";
      Employee emp = null;
      try {
         emp = new Employee("Krishna", 25, filePath);
      }
         System.out.println("Specified file not found");
      }
      emp.display();
   }
}

Output Result

Name:  Krishna
Age: 25