The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Exceptions

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Throwing exceptions from constructors

Posted by Mark E. on May 31, 2001 at 11:23 AM

My technical lead claims that throwing an exception from a constructor in Java may leave allocated memory that is not cleaned up and that we should do very little in a constructor and avoid throwing exceptions. I believe this to be wrong but could not find any definition of what happens to the allocated memory when the initialization of a class is interrupted by throwing an exception. Anyone know what the answer is? I prefer not having a two step process to initialize an object because then I have to put in initialization check in all the functions. Also, what is the value of a reference variable when initialzation is interupted? Does Java set it to null or just leave it what it was before? If it is the second, then I suppose that the reference could still be pointing to a valid object; assuming it was pointing to one before the attempt to initialize the new object:
MyClass objRef = new MyClass(goodArgument); // no exception
objRef = new MyClass(badArgument); // throws exception, what is objRef set to???



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us