Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an object constructed if an initializer throws?

I was reading This Article over on Jag Reeghal's blog and It seemed to me that what he was suggesting was really not the same thing as using an object initializer. Then I realized that I didn't really know for sure.

When an object is constructed, with object initializers, and one of those intitializers throws (maybe a Null Reference exception)... is the object actually constructed? Is this basically like an exception being thrown in the constructor? Or is the object fully constructed, and then initialized?

like image 808
Erik Funkenbusch Avatar asked Apr 18 '11 00:04

Erik Funkenbusch


People also ask

What happens when an exception is thrown in a constructor?

When throwing an exception in a constructor, the memory for the object itself has already been allocated by the time the constructor is called. So, the compiler will automatically deallocate the memory occupied by the object after the exception is thrown.

Do constructors initialize an object?

Constructors initialize the attributes in newly created objects. They have the same name as the class. A constructor signature is the constructor name followed by the parameter list which is a list of the types of the parameters and the variable names used to refer to them in the constructor.

Is constructor used for initialization?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type.

Can we use throws with constructor?

The short answer to the question “can a constructor throw an exception in Java” is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results and optimizing your code.


2 Answers

An object initilizer statement like var x = new Foo { Property1 = 5}; will be implemented as something like this:

Foo temp = new Foo();
temp.Property1 = 5;
x = temp;

As you can see, the properties in the initiallizer are set after the object is constructed, however the variable isn't set to the fully-initialized object until all the properties are set, so if an exception is thrown, the constructed object is lost even if the exception is caught (the variable will remain null or whatever value it had before).

like image 97
Mark Cidade Avatar answered Sep 21 '22 03:09

Mark Cidade


It is fully constructed first, then initialized. You will however never get a reference to such an object if an exception is thrown, the compiler makes sure that your reference can only ever refer to a properly initialized object. It uses a temporary to guarantee this.

So for example, this code:

var obj = new Model {
   FirstName = reader[0].ToString(),
   LastName = reader[1].ToString(),
   Age = Convert.ToInt32(reader[2].ToString())
};

Is rewritten by the compiler to:

var temp = new Model();
temp.FirstName = reader[0].ToString();
temp.LastName = reader[1].ToString();
temp.Age = Convert.ToInt32(reader[2].ToString())
var obj = temp;
like image 21
Hans Passant Avatar answered Sep 19 '22 03:09

Hans Passant