Why should I have written ZeroMQ in C, not C++ (2012)
I think there&#x27;s a lot of merit to this post, but it really shows its age. Modern C++ typically suffers from only the last point raised - exceptions during destruction, but that goes against the all good C++ practices.<p>Overall, the author&#x27;s approach to exception use is flawed. It&#x27;s bad form to use exceptions for code flow purposes, as per their example. If you throw an exception in the same block that catches the exception, you&#x27;ve wasted a whole lot of time doing something an IF-statement could solve trivially.<p>All projects that I have worked on that use C++ has declared that exceptions should be used in exceptional circumstances only - that is, when you cannot handle the error in the current code block and the function interface does not support returning enough information to the caller to detail what went wrong. An exception is more than just throwing your hands in the air and saying &quot;I can&#x27;t do that&quot; - it has context, purpose, history. Getting rid of exceptions breaks some of the fundamental design concepts of the language.<p>2-step initialisation is the only way around errors during construction whilst forbidding exceptions. But 2-step init breaks a fundamental rule of RAII - the constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done. If the constructor is not allowed to throw exceptions, as per the language&#x27;s standardised interface for constructors, then my library or application needs to be modified to support whatever process a third party library has defined as appropriate. There is a wide surface for bugs to creep in, let alone costing me time, money and effort in supporting whatever interface they&#x27;ve come up with.<p>If an object has been constructed, it should be in a valid state unless an exception has been thrown, in which case I&#x27;m told what went wrong. If I can fix it before the stack is fully unwound, I can save the day. but if there&#x27;s nothing I can do, the exception has to roll to the top as that&#x27;s the only other option. That then begs the question; should I catch all exceptions at the top level and prevent crashing, or should I crash and allow whatever system I&#x27;m running under to restart me? That depends on the project, but typically I&#x27;d let it go. systemd should bring me back, docker should restart me, kubernetes should restart my pod, etc etc. If I focus on what is within my control, and delegate everything else, the system will be cleaner and much more maintainable.<p>I&#x27;ve never come across a situation where exceptions during destruction is a problem, but am very interested in any examples. C++ standards define that you _can_ throw exceptions, but you shouldn&#x27;t for the exact reason raised in the article - the process will be terminated as there&#x27;s nothing else that can be done. If there aren&#x27;t any destructors containing the throw keyword, it&#x27;s not likely to throw an exception - OOM or other system exceptions are still possible, but why are you allocating memory in a destructor? destructors just need to release resources and clear down the object, it shouldn&#x27;t be requesting more resources. Thinking about saving the object state before exit? wrong place to do it.