Constructor Exceptions in C++, C#, and Java

Herb Sutter wrote an interesting article on Constructor Exceptions in C++, C#, and Java . Its an interesting read link to entry here

A few excerpts from the article here
  1. A constructor conceptually turns a suitably sized chunk of raw memory into an object that obeys its invariants. An object’s lifetime doesn’t begin until its constructor completes successfully. If a constructor ends by throwing an exception, that means it never finished creating the object and setting up its invariants — and at the point the exceptional constructor exits, the object not only doesn’t exist, but never existed.
  2. A destructor/disposer conceptually turns an object back into raw memory. Therefore, just like all other nonprivate methods, destructors/disposers assume as a precondition that “this” object is actually a valid object and that its invariants hold. Hence, destructors/disposers only run on successfully constructed objects.
PS : If you dont know who is Herb Sutter , read more here

0 opinions: