| Author |
exception from Constructor
|
Vijay Kumar
Ranch Hand
Joined: Jul 24, 2003
Posts: 260
|
|
Hi all, Is it good practice to throw exceptions from constuctors. Regards Vijay
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12926
|
|
My opinion: A constructor should always initialize the object so that it is in a well-defined state. If an error occurs in the constructor which prevents the object from being initialized properly, then the constructor should throw an exception.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Jan van Mansum
Ranch Hand
Joined: Oct 19, 2007
Posts: 74
|
|
I think so. It is the best way I know to write a constructor that can fail. The other way would be to set some error field on the object and tell the client of the class to first check if construction was successful, which I think is ugly. Compare: and The Java compiler will oblige you to either handle SomeException yourself or add it to the throws declaration of your function, which will require the callers of your function to handle it (or either ... etc). There is no need for the user of your class to remember to check whether construction was successful. OK, if have still not convinced you, look at the Java API. Here are some examples where API constructors that throw exceptions. java.net.URL java.net.URI A lot of Java API constructors also throw IllegalArgumentException. This is a descendent of RunTimeException and therefore you are not required to handle it. HTH, Jan van Mansum. [ October 25, 2007: Message edited by: Jan van Mansum ]
|
SCJP 1.4, SCWCD 1.4
|
 |
Vijay Kumar
Ranch Hand
Joined: Jul 24, 2003
Posts: 260
|
|
Hi, I have abstract class constructor which is initializing the state of an protected member of this abstract class which will be available to all sub class. This constructor UnMarshalling a xml file which throws an JAXBException. Is it right way to do it. If I don't provide unmarshalling in constructor then every sub class will call unMarshalling explicitly. Regards Vijay
|
 |
Jan van Mansum
Ranch Hand
Joined: Oct 19, 2007
Posts: 74
|
|
OK, if I understand you correctly, you want the abstract base class to do the unmarshalling of the XML so that all the derived classes don't have to do that. That is possible. You can have the base class constructor and all the derived class constructors throw a JAXBException or - better - some application specific exception with the JAXBException wrapped in it (see my previous post).
|
 |
Vijay Kumar
Ranch Hand
Joined: Jul 24, 2003
Posts: 260
|
|
Hi Jan van Mansum, you understnad correctly what I want to say.Some time its require to throw an exception from the constructor So that sub class can through the same error or it can wrap that exception. My idea was that without unmarshalling the xml concrete class object should not be created. Thanks Vijay Kumar SCJP 1.4
|
 |
 |
|
|
subject: exception from Constructor
|
|
|