Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exception from Constructor  RSS feed

 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Is it good practice to throw exceptions from constuctors.

Regards
Vijay
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!