Стандарт C++ 98 (1119566), страница 81
Текст из файла (страница 81)
[Example:struct B {virtual void f() throw (int, double);virtual void g();};struct D: B {void f();void g() throw (int);};// ill-formed// OKThe declaration of D::f is ill-formed because it allows all exceptions, whereas B::f allows only int anddouble. ] Similarly, any function or pointer to function assigned to, or initializing, a pointer to functionshall only allow exceptions that are allowed by the pointer or function being assigned to or initialized.296© ISO/IECISO/IEC 14882:1998(E)15 Exception handling15.4 Exception specifications[Example:class A { /* ... */ };void (*pf1)();void (*pf2)() throw(A);void f(){pf1 = pf2;pf2 = pf1;}// no exception specification// OK: pf1 is less restrictive// error: pf2 is more restrictive—end example]4In such an assignment or initialization, exception-specifications on return types and parameter types shallmatch exactly.
In other assignments or initializations, exception-specifications shall match exactly.5Types shall not be defined in exception-specifications.6An exception-specification can include the same type more than once and can include classes that arerelated by inheritance, even though doing so is redundant. An exception-specification can also include theclass std::bad_exception (18.6.2.1).7If a class X is in the type-id-list of the exception-specification of a function, that function is said to allowexception objects of class X or any class publicly and unambiguously derived from X. Similarly, if apointer type Y* is in the type-id-list of the exception-specification of a function, the function allows exceptions of type Y* or that are pointers to any type publicly and unambiguously derived from Y.
Otherwise, afunction only allows exceptions that have the same type as the types specified in the type-id-list of itsexception-specification.8Whenever an exception is thrown and the search for a handler (15.3) encounters the outermost block of afunction with an exception-specification, the function unexpected() is called (15.5.2) if the exceptionspecification does not allow the exception. [Example:classclassclassclassX { };Y { };Z: public X { };W { };void f() throw (X, Y){int n = 0;if (n) throw X();if (n) throw Z();throw W();}// OK// also OK// will call unexpected()—end example]9The function unexpected() may throw an exception that will satisfy the exception-specification forwhich it was invoked, and in this case the search for another handler will continue at the call of the functionwith this exception-specification (see 15.5.2), or it may call terminate().10An implementation shall not reject an expression merely because when executed it throws or might throwan exception that the containing function does not allow.
[Example:extern void f() throw(X, Y);void g() throw(X){f();}// OKthe call to f is well-formed even though when called, f might throw exception Y that g does not allow. ]297ISO/IEC 14882:1998(E)15.4 Exception specifications© ISO/IEC15 Exception handling11A function with no exception-specification allows all exceptions. A function with an empty exceptionspecification, throw(), does not allow any exceptions.12An exception-specification is not considered part of a function’s type.13An implicitly declared special member function (clause 12) shall have an exception-specification. If f is animplicitly declared default constructor, copy constructor, destructor, or copy assignment operator, itsimplicit exception-specification specifies the type-id T if and only if T is allowed by the exceptionspecification of a function directly invoked by f’s implicitly definition; f shall allow all exceptions if anyfunction it directly invokes allows all exceptions, and f shall allow no exceptions if every function itdirectly invokes allows no exceptions.
[Example:struct A {A();A(const A&) throw();~A() throw(X);};struct B {B() throw();B(const B&) throw();~B() throw(Y);};struct D : public A, public B {// Implicit declaration of D::D();// Implicit declaration of D::D(const D&) throw();// Implicit declaration of D::~D() throw (X,Y);};Furthermore, if A::~A() or B::~B() were virtual, D::~D() would not be as restrictive as that ofA::~A, and the program would be ill-formed since a function that overrides a virtual function from a baseclass shall have an exception-specification at least as restrictive as that in the base class.
]15.5 Special functions1The exception handling mechanism relies on two functions, terminate() and unexpected(), forcoping with errors related to the exception handling mechanism itself (18.6).15.5.1 The terminate() function1[except.special][except.terminate]In the following situations exception handling must be abandoned for less subtle error handling techniques:— when the exception handling mechanism, after completing evaluation of the expression to be thrown butbefore the exception is caught (15.1), calls a user function that exits via an uncaught exception,134)— when the exception handling mechanism cannot find a handler for a thrown exception (15.3), or— when the destruction of an object during stack unwinding (15.2) exits using an exception, or— when construction or destruction of a non-local object with static storage duration exits using an exception (3.6.2), or— when execution of a function registered with atexit exits using an exception (18.3), or— when a throw-expression with no operand attempts to rethrow an exception and no exception is beinghandled (15.1), or— when unexpected throws an exception which is not allowed by the previously violated exceptionspecification, and std::bad_exception is not included in that exception-specification (15.5.2), or— when the implementation’s default unexpected_handler is called (18.6.2.2)__________________134) For example, if the object being thrown is of a class with a copy constructor, terminate() will be called if that copy constructor exits with an exception during a throw.298© ISO/IEC15 Exception handling2ISO/IEC 14882:1998(E)15.5.1 The terminate() functionIn such cases,void terminate();is called (18.6.3).
In the situation where no matching handler is found, it is implementation-definedwhether or not the stack is unwound before terminate() is called. In all other situations, the stack shallnot be unwound before terminate() is called. An implementation is not permitted to finish stackunwinding prematurely based on a determination that the unwind process will eventually cause a call toterminate().15.5.2 The unexpected() function1[except.unexpected]If a function with an exception-specification throws an exception that is not listed in the exceptionspecification, the functionvoid unexpected();is called (18.6.2) immediately after completing the stack unwinding for the former function2The unexpected() function shall not return, but it can throw (or re-throw) an exception. If it throws anew exception which is allowed by the exception specification which previously was violated, then thesearch for another handler will continue at the call of the function whose exception specification was violated.
If it throws or rethrows an exception that the exception-specification does not allow then the following happens: If the exception-specification does not include the class std::bad_exception (18.6.2.1)then the function terminate() is called, otherwise the thrown exception is replaced by animplementation-defined object of the type std::bad_exception and the search for another handlerwill continue at the call of the function whose exception-specification was violated.3Thus, an exception-specification guarantees that only the listed exceptions will be thrown. If theexception-specification includes the type std::bad_exception then any exception not on the list maybe replaced by std::bad_exception within the function unexpected().15.5.3 The uncaught_exception() function1[except.uncaught]The functionbool uncaught_exception()returns true after completing evaluation of the object to be thrown until completing the initialization ofthe exception-declaration in the matching handler (18.6.4).
This includes stack unwinding. If the exception is rethrown (15.1), uncaught_exception() returns true from the point of rethrow until therethrown exception is caught again.15.6 Exceptions and access[except.access]1If the exception-declaration in a catch clause has class type, and the function in which the catch clauseoccurs does not have access to the destructor of that class, the program is ill-formed.2An object can be thrown if it can be copied and destroyed in the context of the function in which thethrow-expression occurs.299ISO/IEC 14882:1998(E)©(Blank page)300ISO/IEC© ISO/IECISO/IEC 14882:1998(E)16 Preprocessing directives16 Preprocessing directives16 Preprocessing directives1[cpp]A preprocessing directive consists of a sequence of preprocessing tokens. The first token in the sequence isa # preprocessing token that is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character.