views:

469

answers:

7

What kind of answers would you accept to the following question

"Describe the process and/or pitfall of throwing exceptions from constructor and destructors" (C++/C#/java)

What amount of knowledge about this would you consider essential, for a candidate claiming to have several years of experience in any of these languages (if he misses this, you almost certainly don't want him).

+2  A: 

How about this (I am referring to C++): If an object throws within its constructor, the object may be left at an undefined state. The destructor will not run. This may lead to memory and handle leaks possibly even deadlocks.

Adrian Grigore
At least in C++, if an exception is thrown from a ctor, the object has never come into existence (which is why the dtor is not invoked) - it cannot therefore be in an undefined state.
anon
But it does not free resources it might have allocated, such as malloc of child objects. These objects still exist and define part of the partent object's state. Therefore, even though the object does not exist, part of it's state still does and it sure is undefined (leaked).
Adrian Grigore
+4  A: 

He must know that in C++ the destructor is only called for fully constructed objects. So that the following class is leaky in C++:

class Leaky {
public:
    Leaky() { a = new char[100]; b = new char[100]; }
    ~Leaky() { delete a; delete b; }
private:
   char* a;
   char* b;
};

If exception is thrown while executing b = new char[100] then the Leaky::~Leaky() is not called and you've got a memory leak.

sharptooth
But surely no competent C++ programmer would ever write code like that :-)
anon
Surely no competent C++ programmer is catching exceptions? I thought you were supposed to let them return the the OS so it could free your memory ;) (*April First Here*)
Adam Hawes
@Adam: If you ever need a class like the above you should use a smart pointer. If you try to play with exceptions handling you'll get extremely messy code.
sharptooth
A: 

I don't believe this is a sensible question, at least nor with regard to the three languages mentioned. C++ has RAII, whereas the other two don't, so answers regarding C++ will be completely different.

anon
For a company developing in both C++ and C# (and that happens rather often) a person who understands the difference might be of interest.
sharptooth
+1  A: 

Also, in C++, if an exception is unwinding the stack, and another exception is thrown from a destructor, the program will terminate (or, to be exact, call the terminate_handler).

CAdaker
Yes, I consider this is essential knowledge.
ripper234
A: 

Can't you put the constructor code in try-catch-finally block in C#? This way, if something goes wrong, the code in finally-section will free the resources.

User
A: 

This is kind of an edge question. For example, a competent developer will probably have figured out it's a bad idea somewhere along the line, and just avoids them. Frankly, the best I would've done on this is something like, well, it may leave the object in a partially constructed state, so it's generally best to avoid it." I would prefer to have them elaborate on what they do that's a good practice, and why.

Jack BeNimble
It is not a bad idea to throw an exception from a constructor - in C++ it is best practice if an error occurs during construction.
anon
Still, I would prefer to avoid the entire problem by putting actions that might throw in a separate init() method that can be called after constructing the object.
Adrian Grigore
That would be a very, very bad idea and contrary to accepted C++ wisdom.
anon
Ah, ok. I'm still learning C++.
Jack BeNimble
+6  A: 

If someone said they had 2 years of experience in C++ but didn't understand the ins and outs of constructors / destructors and exceptions, it would not prevent me from hiring them. This is the type of knowledge that can be easily learned and consists of a few simple rules.

I would certainly like them to have that knowledge but not having it does not make them a bad or lazy programmer. I've worked with excellent C++ programmers that spent the majority of their time in old school COM and HRESULT land. This makes heavy use of C++ but virtually eliminates C++ exceptions. There is no need or reason for them to have learned it. Once we introduced exceptions into the code base, it took about 15 minutes to get everyone on board with the constructor / destructor problem.

I would much rather use an interview to test a persons problem solving skills than to see if they knew the ins and outs of a particular part of C++. It is so easy to teach that, but so hard to teach problem solving.

JaredPar