Exceptions in constructors
Posted
by FredOverflow
on Stack Overflow
See other posts from Stack Overflow
or by FredOverflow
Published on 2010-04-14T09:07:37Z
Indexed on
2010/04/14
9:13 UTC
Read the original article
Hit count: 407
In C++, the lifetime of an object begins when the constructor finishes successfully. Inside the constructor, the object does not exist yet.
Q: What does emitting an exception from a constructor mean?
A: It means that construction has failed, the object never existed, its lifetime never began. [source]
My question is: Does the same hold true for Java? What happens, for example, if I hand this
to another object, and then my constructor fails?
Foo()
{
Bar.remember(this);
throw new IllegalStateException();
}
Is this well-defined? Does Bar
now have a reference to a non-object?
© Stack Overflow or respective owner