Search code examples
c++exceptionerror-handlingtry-catch

Is it good practice to silence an exception with an empty catch block?


Is this considered good programming practice in C++:

try {
    // some code
}
catch(someException) {
    // do something
}
catch (...) 
{
    // left empty <-- Good Practice???
} 

Solution

  • No! That is a terrible practice!

    Just about the only time you should catch (...) and not rethrow the exception would be in main() to catch any otherwise unhandled exceptions and to display or log an error before exiting.

    If you catch (...), you have absolutely no idea what exception was thrown, and thus you can't know whether it is safe to continue running.