- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can constructor throw exceptions in Java?
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
- Related Articles
- Can a constructor throw an exception in Java?
- Can a constructor be synchronized in Java?
- Can a constructor be overridden in java?
- How can I add condition in custom exceptions in java?
- Can a constructor be made final in Java?
- Can we define a static constructor in Java?
- Can we declare constructor as final in java?
- Can we have a constructor private in java?
- Can we throw an object of generic class in java?
- Can we write any code after throw statement in Java?
- Can a method throw java.lang.Exception without declaring it in java?
- Custom exceptions in Java
- Can we declare a constructor as private in Java?
- Can we define constructor inside an interface in java?
- Throw and throws in Java
