The parameter "catch (...)" C ++

I recently saw something interesting in C ++ code:

try { //doStuff } catch ( ... ) { //doStuff } 

"..." is what I am talking about.

Now, at first glance, you might think that this is nothing more than a filler, like a comment similar to the "doStuff" that we see. The strange thing is that writing this in an Eclipse CDT really works without giving any syntax errors.

Is there a special purpose for this?

+6
source share
4 answers

As already mentioned, he catches everything. From what I saw, this is mostly used when you cannot determine the actual exception that was thrown. And this can happen if this exception is a Structured exception that is not C ++. For example, if you try to access some invalid memory location. Itโ€™s usually not a good habit to use these catch everyone. You don't have a (portable) way to get the stack trace, and you don't know anything about the exception.

Using this for reasons other than examples or very trivial cases may indicate that the author is trying to hide the instability of the program without worrying about unrecognized exceptions. If you ever encounter such a thing, you better allow the program to crash and create a crash dump, which you can analyze later. Or use a structured exception handler (if you use VS, you donโ€™t know how this is done for other compilers).

+6
source

This is catch All .
He will catch any type of eliminated throw.
During use, make sure it is placed at the end of all catch handlers, or it will simply catch all of your exceptions, regardless of type.

+6
source

This is โ€œcatch the ellipsis,โ€ which means โ€œcatch all the exceptions that have been thrown and handle them here.โ€ Unlike catch( SpecificType ) , which will only catch exceptions of certain types of catch(...) , it will catch all C ++ exceptions.

+3
source

If there are some exceptions that can be returned from the try block that you may not know about or do not want to address specifically, you can put this code. He will take advantage of all the exceptions.

+1
source

Source: https://habr.com/ru/post/893043/


All Articles