I agree with Raedwald that if you use C ++ without a very careful coding standard in order to avoid EH (for example: using nothrow new, avoiding standard containers, etc.), which I assume that the legacy code wasn’t, then the code is already broken and is likely to leak and do sporadic things before the exceptions it may already encounter, like bad_alloc or bad_cast , if you rely on dynamic_cast .
However, from a very pragmatic point of view with an outdated code base, there is a chance that the outdated code could get away from it. And in the end, how many non-trivial applications can gracefully recover from bad_alloc exceptions without much control over memory allocation? Not so much, and this does not lead to the fact that the whole world comes to a screeching stop.
Therefore, I do not recommend rewriting legacy code to try to catch exceptions and use RAII everywhere. You can use RAII here and there in code that you absolutely must change, but I would try to find reasons not to change it too much. Write tests for this and try to stabilize it and turn it into a black box; the library of functionality that will be used is not supported and changes when passing endlessly through an infinite LOC.
Now, the main reason I settled here is to resurrect this old thread (apologies!) Because of this comment:
In general, the code checks for division by 0 or checks for NULL pointers before dereferencing. But still it happens that such a thing will be. Since dividing by zero or dereferencing a NULL pointer does not throw an exception [...]
In my strong opinion, you should not throw things like zero pointer or divide by zero, because these are programmer errors. If you don’t work in mission-critical software where you want to elegantly restore functionality, even if the software doesn’t work to try to reduce the risk of life costs or something like that, you don’t want the application to elegantly recover in a programming event mistakes. The reason you usually don’t want to do this is because it has the disadvantage of hiding errors, which makes them quiet, allowing users to ignore and treat them, or even not report them.
Instead of programmer errors, you usually prefer assert , which generally does not include exceptions. If the assertion fails, the software in the debug build will stop and will usually display an error message telling you where the assertion did not work until the exact line of code. This is usually the fastest rate for detecting and fixing these programming errors when starting the debugger in response to an error report, so feel free to assert .
Exceptions are most useful for external exceptional events outside the control of the programmer. An example would be reading a file that turns out to be damaged. What is not in the programmer’s controller to process, so that it is convenient to drop and restore. Another example is the inability to connect to the server, the user placing the interrupt button for the operation to be completed, etc. These are exceptional external input events, not programmer errors.
The best way to repair programmer errors, such as null access pointer and divide by zeros, is to first find them ( assert convenient), write a test to play it, fix and pass the test, and call it a day, and not throw exceptions and catch them leaving these errors there.