
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can a constructor throw an exception in Java?
Yes, constructors are allowed to throw an exception in Java.
A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class. Each object of a class will have its own state (Instance variables) and access to methods of its class.
Throw an Exception from a Constructor
- A checked exception can be used to indicate a legitimate problem when trying to create an instance, while an unchecked exception typically indicates a bug either in the client code or in the constructor itself.
- In both cases, an object is actually allocated in the heap space, but a reference to it is not returned. The object remains in a partially initialized state until it gets garbage collected.so we conclude that saving a reference to the object from the constructor itself (by using this reference) is a risky thing, since we may give access to an object in an invalid state.
- Another important thing to note about the exception in a constructor is related to reflection. When we need to invoke the empty constructor using a class object for example test, we sometimes use the method test.newInstance().
- Any exception thrown by the constructors is propagated without a change. In other words, the newInstance() method may throw checked exception that it does not even declare.
Example
public class ConstructorExceptionTest { public ConstructorExceptionTest() throws InterruptedException { System.out.println("Preparing an Object"); Thread.sleep(1000); System.out.println("Object is ready"); } public static void main(String args[]) { try { ConstructorExceptionTest test = new ConstructorExceptionTest(); } catch (InterruptedException e) { System.out.println("Got interrupted..."); } } }
Output
Preparing an Object Object is ready
- Related Articles
- Can constructor throw exceptions in Java?
- Can we throw an Unchecked Exception from a static block in java?
- Can the abstract methods of an interface throw an exception in java?
- While chaining, can we throw unchecked exception from a checked exception in java?
- While overriding can the subclass choose not to throw an exception in java?
- \nHow to throw an exception from a static block in Java? \n
- How do you throw an Exception without breaking a for loop in java?
- How can I get a JavaScript stack trace when I throw an exception?
- Is it possible to throw exception without using "throws Exception" in java?
- Throw Custom Exception in Kotlin
- How to throw a C++ exception?
- How do I manually throw/raise an exception in Python?
- What is the difference between throw e and throw new Exception(e) in catch block in java?
- Can we throw an object of generic class in java?
- Can we define constructor inside an interface in java?

Advertisements