Re: How to handle exceptions in constructor?



George via JavaKB.com wrote:
Hello everyone,


When exceptions occur in constructor so that the instance of the object can not be created, should we throw the exception to the invoker of the constructor or simply catch the exception (and not throw it again)?

I think we should not catch the exception (and not throw it again), since in
this approach, the invoker can not see whether the instance of the object has
been created successfully.

I think we should throw the exception to let the invoker know that the
instance of the object has not been created successfully, so that the invoker
can peform appropriate actions.
I think so, too.
... But I am not quite sure whether I am correct
since I have not seen any constructors which throw exceptions before.
There are many, for example the constructors of java.io.FileInputStream, java.net.URL or java.rmi.server.UnicastObject .

Could anyone help?

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

.



Relevant Pages

  • How to handle exceptions in constructor?
    ... When exceptions occur in constructor so that the instance of the object can ... should we throw the exception to the invoker of the ... the invoker can not see whether the instance of the object has ... Prev by Date: ...
    (comp.lang.java.help)
  • Re: Bjarnes exception safe sample
    ... "George" wrote: ... he means throwing an exception from T's copy constructor. ... The assignment operator may fail by throwing an exception from T's copy constructor. ... Alex ...
    (microsoft.public.vc.language)
  • Re: How to handle exceptions in constructor?
    ... Stefan Schulz wrote: ... >> I think we should throw the exception to let the invoker know that the ... >> instance of the object has not been created successfully, so that the invoker ... >> since I have not seen any constructors which throw exceptions before. ...
    (comp.lang.java.help)
  • Re: How to handle exceptions in constructor?
    ... > instance of the object has not been created successfully, so that the invoker ... > since I have not seen any constructors which throw exceptions before. ... You absolutely should pass the exception through, ... Prev by Date: ...
    (comp.lang.java.help)
  • Re: DbC & Exceptions & Style
    ... exception. ... Different ways the constructor can fail ... to avoid writing code that converts GUI data into objects, ... All domain objects must now provide a default constructor. ...
    (comp.object)