catch blocks (C++ only)

Read syntax diagramSkip visual syntax diagramcatch block syntax
 
>>-catch--(--exception_declaration--)--{--statements--}--------><
 

You can declare a handler to catch many types of exceptions. The allowable objects that a function can catch are declared in the parentheses following the catch keyword (the exception_declaration). You can catch objects of the fundamental types, base and derived class objects, references, and pointers to all of these types. You can also catch const and volatile types. The exception_declaration cannot be an incomplete type, or a reference or pointer to an incomplete type other than one of the following:

You cannot define a type in an exception_declaration.

You can also use the catch(...) form of the handler to catch all thrown exceptions that have not been caught by a previous catch block. The ellipsis in the catch argument indicates that any exception thrown can be handled by this handler.

If an exception is caught by a catch(...) block, there is no direct way to access the object thrown. Information about an exception caught by catch(...) is very limited.

You can declare an optional variable name if you want to access the thrown object in the catch block.

A catch block can only catch accessible objects. The object caught must have an accessible copy constructor.

Related information