Freigeben über


Catchable Types

Because C++ enables you to throw exceptions of any type, you need to determine which catch handlers can catch an exception of a specific class type. A C++ exception can be caught by a catch handler that specifies the same type as the thrown exception, or by a handler that can catch any type of exception. An exception can also be caught by a catch handler that uses a reference to the same type as the thrown exception.

If the type of thrown exception is a class, which also has a base class (or classes), it can be caught by handlers that accept base classes of the exception's type, as well as references to bases of the exception's type. Note that when an exception is caught by a reference, it is bound to the actual thrown exception object; otherwise, it is a copy (much the same as an argument to a function).

When an exception is thrown, it may be caught by the following types of catch handlers:

  • A handler that can accept any type (using the ellipsis syntax).

  • A handler that accepts the same type as the exception object; because it is a copy, const and volatile modifiers are ignored.

  • A handler that accepts a reference to the same type as the exception object.

  • A handler that accepts a reference to a const or volatile form of the same type as the exception object.

  • A handler that accepts a base class of the same type as the exception object; since it is a copy, const and volatile modifiers are ignored. The catch handler for a base class must not precede the catch handler for the derived class.

  • A handler that accepts a reference to a base class of the same type as the exception object.

  • A handler that accepts a reference to a const or volatile form of a base class of the same type as the exception object.

  • A handler that accepts a pointer to which a thrown pointer object can be converted via standard pointer conversion rules.

See Also

Reference

C++ Exception Handling