Don’t catch what you can’t handle

One of the cardinal rules of C++ exception handling is “don’t catch what you can’t handle”. Of course there are always, er, exceptions to the rule, but the basic principle always holds. The consequences of violating the rule are less severe in the .NET world, but even there it’s a good rule of thumb.

Back in the old days you needed permission from a deity before you could use catch(…) to catch all exceptions. By the turn of the century, an Executive Order was sufficient. These days I see posts all the time on programming forums that go something like “My plugin crashes when I call ThirdParty::Function(). How can I catch all exceptions in ThirdParty.dll?” Bzzzt. Foolish programmer alert!

The .NET framework allows exceptions to be used for signaling, so inexperienced programmers often think that’s how they work in C++ as well. I think this is a case where the less rigorous .NET programming model bleed-over effect has negatively impacted C++ programming.

Leave a Reply

Your email address will not be published. Required fields are marked *