Can constructor throw exceptions in Java?

Java 8Object Oriented ProgrammingProgramming

A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have same name as their class and, have no return type.

There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.

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

This is the constructor of the class example

Constructor throwing exceptions

Yes, just like methods you can throw exceptions from constructors in. But, if you do so, you need to catch/throw (handle) the exception at the method where you invoke the constructor. If you don’t a compile time error is generated.

Example

In the following example we have a class named Employee whose constructor throws an IOException, we are instantiating this class without handling the exception. Therefore, if you compile this program, it generates a compile 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);
   }
}

Compile time error

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

Example

To make this program work properly, wrap the instantiation line within try-catch or, throw the 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);
      }catch(IOException ex) {
         System.out.println("Specified file not found");
      }
      emp.display();
   }
}

Output

Name: Krishna
Age: 25
raja
Published on 06-Aug-2019 11:28:59
Advertisements