What\'s the best technique for exiting from a constructor on an error condition in C++? In particular, this is an error opening a file.
Thanks for the responses. I\'
If the object that you're constructing is invalid due to the error, and needs to be disposed of by the caller, then you pretty much have to throw an exception. This allows the compiler to perform the proper deallocation of resources.
(Writing exception-safe constructors requires a bit of care -- in brief, you need to use the initializer lists wherever you can, rather than using the constructor body -- but it's critical if you have a case like this, where throwing an exception is a significant possibility.)