                              11.  
                            11.1.  

      ,   ,   ,
 ,  , .  ,   ,
 -  iStack,    4.15, 
        .
      throw   pop()?   :

   // ,    
   throw popOnEmpty;

 ,  .    ,   pop() 
    .    throw 
   .      
 .  throw   pop()   :

   //    
   throw popOnEmpty();

   ,  - pop()  push()    
  bool: true ,    ,  false  
 .       pop()  push()
 ,   .

                              11.2. try-

            iStack 
 - pop()  push().  50   for.  
    ,  3: 3, 6, 9  ..    4 (4,
8, 12...),     ,    10 (10, 20, 30...),  
   ,      .
        .     
  ,    ,  
 .   ,     ,
    .     

534

  ,    .  ,   

   Try-      C++:  ,  
.     ,     
    ,      catch-. ,
 main()   ,    stack  
try-.         catch- :

                          11.3.  

(  19  ,      :   
       .)

   ,      C++ :   
 ,      ,   
.        - pop(),
 .

                           11.3.1. -

   -     ,    throw 
    ,   ,    . :

537

   enum EHstate { noErr, zeroOp, negativeOp, severeError };
   enum EHstate state = noErr;

   int mathFunc( int i ) {
      if ( i == 0 ) {
         state = zeroOp;
         throw state; //  -
      }
      //    

       state     -. 
  throw  -  EHstate, 
    state.   
 ?          
  catch-  .

   void calculate( int op ) {
      try {
         mathFunc( op );
      }
      catch ( EHstate eObj ) {
          // eObj -   -
      }

            .
 eObj   -   , 
        
 . (      
7.3.)
        ,     
.  catch-     -,

          
       ,     
  .

      catch-   -.
    ,    throw,   .
,  eObj  catch-   
 state,    throw:

   void calculate( int op ) {
      try {
         mathFunc( op );
   }
   catch ( EHstate &eObj ) {
      //  ,  
      eObj = noErr; //   state  
   }

   Catch-  eObj  noErr   ,
 .  eObj   ,  ,  
   state.    -
,    throw,   eObj  
state.

                                  11.3.2.  

       ,    catch-,   -
 pop()  iStack.   throw  pop()    try-
,    pop(),   . 
 ,  pop(),   main().  pop() 
main()   try-,   ,     
    catch  . 
  popOnEmpty ,     .

   ,       
       catch, 
  ,   .   
   ,     
 ,    . C++ ,   
      ,  
 -  . (      
19.)

        catch,   , 
 .       , 
    . ,    ,
  terminate()    C++.  
terminate()   abort(),    .
(    abort()    .
    ,  
terminate().   ,    [STROUSTRUP97].)

    , , ,        
.  throw    ,   catch -
  .      
  ,  ,    ,  
 ,      .

                    11.3.3.   

      catch-   
  .       

   throw;

   -.    
  ,   catch-:


   catch ( exception eObj ) {
      if ( canHandle( eObj ) )
         //  
         return;
      else
         //   ,    
         // catch-
         throw;

       -  .

     eObj   ,  catch-   -
,     eObj      
   -,   
.  ,    -   zeroOp.
      -,    
catch-   :


   catch ( EHstate &eObj ) {
      //  -
      eObj = severeErr;
      //      severeErr
      throw;

    ,       catch- 
 ,      -  
      . (  
   19.2,   ,  catch- 
  .)

                          11.3.4.   

          ,   
 ,    ,      . 
,    ,     
  ,       :


   void manip() {
      resource res;

      res.lock(); //  
      //  
      // ,     
      res.release(); //  ,   
   }

     ,      ,  
.   ,     
 ( ,     ,    
),   ,  
 .    ,   catch,   
   (...)       .
:

   //       
   catch (...) {
      //    


    catch(...)      
.        catch-
  ,        
  :

   void manip() {
      resource res;
      res.lock();
      try {
        //  
        // ,     
      }
      catch (...) {
        res.release();
        throw;
     }
     res.release(); //  ,   

               
  .      ,      
 . (       19.)

   Catch-   ,   ,    . 
  ,  . ,  
catch(...)     catch-,    
  ,      :

   try {
      stack.display();
      for ( int ix = 1; ix < 51; ++x )
      {
         //  ,   
      }
   }
   catch ( pushOnFull ) { }
   catch ( popOnEmpty ) { }
   catch ( ... ) { } //      catch-


                              11.4.  

     - pop()  push()  iStack  ,
   . , ,    
.         
  :

       .
    ,        ,   
    .      
  

545

,    .   ,  
    .
          .  
   throw,      ,  
. ,  -  iStack  ,
  :


   class iStack {
   public:
      // ...
      void pop( int &value ) throw(popOnEmpty);
      void push( int value ) throw(pushOnFull);
   private:
     // ...
   };

    ,    ,    
?      
   ,     ,   
     .   
        . 
  ,    ,  
unexpected()    C++,     
terminate(). (     , 
 unexpected().      .
 . [STRAUSTRUP97].)

    recoup()    string,     
.       , unexpected() 
.

   void recoup( int op1, int op2 ) throw(ExceptionType)
   {
      try {
         // ...
         throw string("we're in control");
      }
      //   
      catch ( string ) {
        //   
      }
   } //  , unexpected()  

          
.     ,    throw 
  .
     ,      :

   extern void no_problem () throw();
 
          ,   
   .
         ,  
,     :

   int convert( int parm ) throw(string)
   {
      //...
      if ( somethingRather )
        //  :
        // convert()     const char*
        throw "help!";

547

    throw   convert()      
  C.  -   const char*. 
  const char*     string.   
  ,   convert()   ,
  unexpected().     throw 
 ,        
string:

   throw string( "help!" );


                  11.4.1.      

   void recoup( int, int ) throw(exceptionType);
   void no_problem() throw();
   void doit( int, int ) throw(string, exceptionType);

   // : ,   
   //  recoup()  pf1, 
   void (*pf1)( int, int ) throw(exceptionType) = &recoup;

   // : ,    
   // no_problem(),  ,
   //   pf2
   void (*pf2)( ) throw(string) = &no_problem;

   // : ,   
   //  doit(),  ,   pf3
   //
   void (*pf3)( int, int ) throw(string) = &doit;


                    11.5.    

        C++   . 
     ,     .  
        
 . ,      
    .   
  ,     ,
       .

        iStack ,    push() 
,   ,  .  ,   ,
  push()    :   
  .   ,      
 .        
   , -, .   -
push(),       :

   void iStack::push( int value )
   {
      //   ,   
      if ( full() )
         _stack.resize( 2 * _stack.size() );

      _stack[ _top++ ] = value;

   }

       pop()     
   ?  ,   stack  
 C++ (    6)     
.   ,     
    .    
     .   ,
      .    
    ,   .
