catch blocks (C++ only)

Read syntax diagramSkip visual syntax diagram
catch block syntax

>>-catch--(--exception_declaration--)--{--statements--}--------><

You can declare a handler to catch many types of exceptions. The objects that a function can catch are declared in the parentheses following the catch keyword (the exception_declaration). You can catch both scalar and class objects. You can also catch cv-qualified objects. An exception declaration can declare an lvalue reference, in which case the exception object is passed by reference to the catch handler. The exception_declaration cannot be an incomplete type, abstract class type, C++11rvalue reference typeC++11, or a reference or pointer to an incomplete type other than the following types:
  • void*
  • const void*
  • volatile void*
  • const volatile void*
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.