throw"Help!";
can be caught by a
handler
of
constchar*
type:
try{// ...}catch(constchar* p){// handle character string exceptions here}
and
class Overflow {public:
Overflow(char,double,double);
};
void f(double x){throw Overflow('+',x,3.45e107);
}
can be caught by a handler for exceptions of type
Overflow:
try{
f(1.2);
}catch(Overflow& oo){// handle exceptions of type Overflow here}
When an exception is thrown, control is transferred to the nearest handler with
a matching type ([except.handle]); “nearest” means the handler
for which the
compound-statement or
ctor-initializer
following the
try
keyword was most recently entered by the thread of control and not yet exited.
Throwing an exception
initializes an object with dynamic storage duration,
called the
exception object.
If the type of the exception object would be
an incomplete type ([basic.types.general]),
an abstract class type ([class.abstract]),
or a pointer to an incomplete type other than
cvvoid ([basic.compound]),
the program is ill-formed.
when an active handler for the exception exits by
any means other than
rethrowing,
immediately after the destruction of the object (if any)
declared in the exception-declaration in the handler;
A thrown exception does not
propagate to other threads unless caught, stored, and rethrown using
appropriate library functions; see [propagation] and [futures].
If the exception handling mechanism
handling an uncaught exception
directly invokes a function that exits via an
exception, the function std::terminate is invoked.
[Example 2: struct C {
C(){}
C(const C&){if(std::uncaught_exceptions()){throw0; // throw during copy to handler's exception-declaration object ([except.handle])}}};
int main(){try{throw C(); // calls std::terminate if construction of the handler's// exception-declaration object is not elided ([class.copy.elision])}catch(C){}} — end example]