This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Handling in Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions Handling in Constructors" Watch "Exceptions Handling in Constructors" New topic

Exceptions Handling in Constructors

Kaushik Subramanya

Joined: Sep 26, 2000
Posts: 9
Can anyone tell me how do I catch and handle exceptions within my constructor.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
It's exactly same as the way you throw/catch exceptions in any method.
Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12769
However, unless the exception is something that you can recover from and still produce a valid object, it is better to let the constructor throw the exception and handle the problem somewhere else.

author of:
It is sorta covered in the JavaRanch Style Guide.
subject: Exceptions Handling in Constructors