I read code in a large project, that includs a lot of code like:
try
{
}
catch(...)
{
}
Literally, in the parenthesis after “catch”, there is “…” in it. Not something like “exception e”.
This makes me a little worry.
Is this practice good or safe?
thanks.
No, this is a terrible practice.
If you
catch(...), you have no idea what you’ve caught. It catches all C++ exceptions (and on some platforms with some settings, it catches other exceptions too, like structured exceptions in Visual C++).If you have no idea what exception was thrown, you have no idea what the state of the system is: how do you know whether it is safe for the program to continue running?
The only two ways that it is absolutely safe to exit a
catch(...)block is to terminate the program or to rethrow the exception (usingthrow;). The latter is occasionally useful if you need to perform some cleanup when an exception is thrown but can’t rely on a destructor.