try { // f } catch (...) { fclose(f); throw; } fclose(f); } , f, , , . , . , . , . : void acquire() { // 1 // ... // n // // n // ... // 1 } , . , . , . , FilePtr, FILE* : class FilePtr { FILE* p; public: FilePtr(const char* n, const char* a) { p = fopen(n,a); } FilePtr(FILE* pp) { p = pp; } ~FilePtr() { fclose(p); } operator FILE*() { return p; } }; FilePtr , FILE*, , fopen() . , . : void use_file(const char* fn) { FilePtr f(fn,"w"); // f } , , . 9.4.1  " ". , . , . , . , , , . , . , , , , . - , " " . , " ". X, : x y (.. -). . , , X , , . FilePtr LockPtr (, , x y ). : class X { FilePtr aa; LockPtr bb; // ... X(const char* x, const char* y) : aa(x), // `x' bb(y) // `y' { } // ... }; , , , , . . , aa bb , aa, bb. , , . , . . : class X { int* p; // ... public: X(int s) { p = new int[s]; init(); } ~X() { delete[] p; } // ... }; , . , init() , . , . : template<class T> class MemPtr { public: T* p; MemPtr(size_t s) { p = new T[s]; } ~MemPtr() { delete[] p; } operator T*() { return p; } } class X { MemPtr<int> cp; // ... public: X(int s):cp(s) { init(); } // ... }; , p, MemPtr. init() , cp. , ++ , operator new() , . , , . , , , . , . . 9.4.2  . , " ". , . ( ). , . , . , , , , . . " " , , . 9.4.3  : , ? , fopen() new , , , . : : . : , , . , . . ++ , - . new: #include <stdlib.h> extern void* _last_allocation; extern void* operator new(size_t size) { void* p; while ( (p=malloc(size))==0 ) { if (_new_handler) (*_new_handler)(); // else return 0; } return _last_allocation=p; } new() , _new_handler(). _new_handler() , . , new, .. . , new: void my_new_handler() { try_find_some_memory(); // // if (found_some()) return; // , throw Memory_exhausted(); // // "_" } - : try { // ... } catch (Memory_exhausted) { // ... } operator new() _new_handler, set_new_handler(). , set_new_handler(&my_new_handler); Memory_exhausted : void (*oldnh)() = set_new_handler(&my_new_handler); try { // ... } catch (Memory_exhausted) { // ... } catch (...) { set_new_handler(oldnh); // // throw(); // } set_new_handler(oldnh); // // , $$9.4 " " catch (...). , my_new_handler(), , , . - , . , , . , -, $$10.4.2 . , - , , , " " , (callback). , , , . , , , , . , . , , , . , ( ). , , . , . 9.4.4  . , , (.. ) : [1] , . [2] , , . , , : Vector::Vector(int size) { if (sz<0 || max<sz) throw Size(); // ... } , , , (Size()) : Vector* f(int i) { Vector* p; try { p = new Vector v(i); } catch (Vector::Size) { // } // ... return p; } . - . . , , , . , , . " " - , , . , , , . 9.5  , , ? , , . , . : class message { /* ... */ }; // class queue { // // ... message* get(); // 0, // ... }; void f1(queue& q) { message* m = q.get(); if (m == 0) { // // ... } // m } : class Empty { } // "_" class queue { // ... message* get(); // Empty, // ... }; void f2(queue& q) { try { message* m = q.get(); // m } catch (Empty) { // // ... } } - . , , . (.. , ), , f2() , (.. ). , , , f2() : class queue { // ... message* get(); // Empty, int empty(); // ... }; void f3(queue& q) { if (q.empty()) { // // ... } else { message* m = q.get(); // m } } , get() , . , . , : . . , , ( ) . , ( , get() , ). - . , , . , , , , (.. ). , if for. , . , , , , . , , message, . , queue int, , . get() $$9.1, . , , : void f(Queue<X>& q) { try { for (;;) { // `` '' // X m = q.get(); // ... } } catch (Queue<X>::Empty) { return; } } , , , , . , . get(). . . , , . - , , , . , , . 9.6  . , : void f(int a) throw (x2, x3, x4); , f() x2, x3 x4, , . , , , , unexpected(). unexpected() terminate(), , , abort(). $$9.7. void f() throw (x2, x3, x4) { // - } void f() { try { // - } catch (x2) { // throw; } catch (x3) { // throw; } catch (x4) { // throw; } catch (...) { unexpected(); } } , , . , , . , . , . , , . int f(); // , , : int g() throw (); // , . , , , , . , . , , . 9.6.1  , unexpected(), , . unexpected() , . , unexpected() . Y , , Yerr. , class someYerr : public Yerr { /* ... */ }; , void f() throw (Xerr, Yerr, IOerr); Yerr . , someYerr f() f() . , . g() . , g() , , unexpected(). g() g(). , g() , , unexpected(). set_unexpected(). , unexpected() " " : typedef void(*PFV)(); PFV set_unexpected(PFV); class STC { // PFV old; // unexpected() public: STC(PFV f) { old = set_unexpected(f); } ~STC() { set_unexpected(old); } }; , unexpected(): void rethrow() { throw; } // // , g(), : void networked_g() { STC xx(&rethrow); // unexpected() rethrow() g(); } , unexpected() catch (...). . , , terminate(). catch (...) , , . , , , " ": void muddle_on() { cerr << " \n"; } // ... STC xx(&muddle_on); // unexpected() // unexpected() , . , . , " " . , . -, , . , unexpected() Fail (): void fail() { throw Fail; } // ... STC yy(&fail); : (.. ), (.. Fail). , , . , , Fail. 9.7  , terminate(). , , , , , . terminate() , set_terminate(): typedef void (*PFV)(); PFV set_terminate(PFV); set_terminate() , . terminate() , . , terminate() , , , . , , , , . unexpected() , , , , . unexpected() , set_unexpected(). unexpected() terminate(), , , abort(). , . , terminate() . , abort() . exit(). , . 9.8  , " ". , , , , . ? : int f(int arg) { try { g(arg); } catch (x1) { // g(arg); } catch (x2) { // return 2; } catch (x3) { // throw; } catch (x4) { // x4 throw xxii; } catch (x5) { // } catch (...) { // terminate(); } // ... } , , . , , , : void f() { int i1; // ... try { int i2; // ... } catch (x1) { int i3; // ... } catch (x4) { i1 = 1; // i2 = 2; // : i2 i3 = 3; // : i3 } } . . , . , . , ( ) . , , . , . . , , . , , . terminate() , , , , . unexpected() , . , .. , , . , , , , , . , : [1] , , , , ; [2] , ( , ); [3] , ; [4] , . , , , , , , . , . . . , errno , , , , , errno -: void callC() { errno = 0; cfunction(); if (errno) throw some_exception(errno); } void fromC() { try { c_pl_pl_function(); } catch (...) { errno = E_CPLPLFCTBLEWIT; } } , . , , . , . , , . , , , " ", , " ". 9.9  1. (*2) STC , . 2. (*3) CheckedPtrToT $$7.10 , . 3. (*3) find char*. , "hello", . . 4. (*1) Int, int , . : . $$9.3.2. 5. (*2) : , , . ++ , , . 6. (*1) Vector Range Size. : . $$9.3. 7. (*1) , 6, . ? 8. (*2.5) Exception , . ? ? ? 9. (*2) , . 10. (*2) Lock () - , . 11. (*1) int main() { /* ... */ } , , abort(). : fromC() $$9.8 .  * 10.  " , " . ++ -. , . - , , , . <iostream.h>. , . 10.1  - . - . , ++ , - . , - , , , , . , ; -, - .