Possible Duplicate:
How can I catch all types of exceptions in one catch block?
In C++ is there a way to catch ALL exceptions? If so, what would the syntax for that be?
Merci
Possible Duplicate:
How can I catch all types of exceptions in one catch block?
In C++ is there a way to catch ALL exceptions? If so, what would the syntax for that be?
Merci
Yes, you can catch all exceptions that were not handled (and not rethrown) by inner try
/catch
blocks by catch
ing ...
:
try {
// do some stuff
} catch (...) {
// catch any exceptions that weren't handled and/or rethrown in the try block
}
You can also combine this with other catch
es, but make sure they are above the catch (...)
or they will be masked by the catch-all (and you should get a compiler error).
You can catch all exceptions like this
try{
// ...
} catch (...) {
// ...
}
However a more useful thing to do is to catch specific exceptions and then finally fall to the (...)
try{
// ...
} catch (const std::exception& ex) {
// ...
} catch (const std::string& ex) {
// ...
} catch (...) {
// ...
}
No, you cannot catch ALL exceptions, because you cannot catch exceptions that have already been caught by another handler nested within your try/catch
block.