0

I had a phone interview for a programming position at a prominent NYC financial data services and media company recently and the gentleman who interviewed me insisted that it was never a good idea to throw an exception from a constructor and this was a policy at the company.

Not sure I'd want to work at a place with such unfortunate restrictions but the experience got me thinking. I can think of at least one situation when I wouldn't use exceptions (in general), and that is when the code can be called by legacy code that doesn't handle exceptions.

Are there other situations, perhaps particularly from constructors?

karly
  • 1
  • 1
    Does this answer your question? [Throwing exceptions from constructors](https://stackoverflow.com/questions/810839/throwing-exceptions-from-constructors) – julka Dec 27 '19 at 01:13
  • @julka that post seems to answer a slightly different question. I assume my interviewer is wrong and it is quite normal and accepted practice to throw exceptions from constructors. However rarely are things "always" or "never". Just wondering what the "exceptions" are. – karly Dec 27 '19 at 01:23
  • It is not a good idea to throw an exception in the constructor of an exception (or when copying it) - the result is normally program termination. Apart from that, most prohibitions I'm aware of on throwing exceptions in constructors are compensating for bad technique - for example, a constructor body allocates several resources without using RAII, and cleanup is difficult or sometimes infeasible if an exception is thrown before all those actions are completed. – Peter Dec 27 '19 at 01:31
  • [cpp faq](https://isocpp.org/wiki/faq/exceptions#ctors-can-throw) hope this answers your question – TruthSeeker Dec 27 '19 at 02:12
  • @peter thanks, I wasn't aware of the exception from exception ctor/copy ctor. Add to the list exceptions from destructors and functions spectifed as not throwing (noexcept). – karly Dec 27 '19 at 02:13

0 Answers0