Reputation: 10137
I recently saw something interesting in some c++ code:
try {
//doStuff
} catch ( ... ) {
//doStuff
}
The "..." is what I'm referring to.
Now, at first glance one might think that this is nothing more than a filler, like a comment similar to the "doStuff" we see. The weird thing is that typing this in the Eclipse CDT actually works, without giving any syntax errors.
Is there a special purpose for this at all?
Upvotes: 8
Views: 3713
Reputation: 206518
It is a catch-all. It will catch any type of exception thrown.
When using it, make sure that it is placed at the end of all catch handlers, because it will just catch all your exceptions irrespective of the type. (In standard C++, it is an error if the catch-all is not the last handler.)
Upvotes: 8
Reputation: 22020
As others have mentioned, it catches everything. From what I've seen, this is mostly used when you can't identify the actual exception that is thrown. And this could happen if that exception is a Structured Exception, which is not a C++ one. For instance, if you try to access some invalid memory location. It is usually not a good habit to use those "catch all"s. You don't have a (portable) way to get a stack trace, and you don't know anything about the exception thrown.
Using this for reasons other than examples or very trivial cases might indicate the author is trying to hide the program's instability by not taking proper care of unrecognized exceptions. If you ever face such a case, you better let the program crash, and create a crash dump you can analyze later. Or, use a structured exception handler (In case you're using VS - don't know how this is done on other compilers).
Upvotes: 8
Reputation: 23095
If there are some exceptions that can be returned from the try block, which you may not be aware of or you do not want to handle specifically, you can put that code. It will catch all the exceptions.
Upvotes: 1
Reputation: 170489
That's "catch ellipsis" which means "catch whatever exceptions were thrown and handle them here". Unlike catch( SpecificType )
which would catch only exceptions of certain types catch(...)
would catch all C++ exceptions.
Upvotes: 3