                                                           ++   529

                                      11

                          11.  

       ,    
     , 
.     ,  ,  , 
   ,    .   ,  
catch-     ,  try-
.        ,   
      ,    
   .     ,
   ,    .

                        11.1.  

          ,   
, :   0,      
 .       , 
    .  C++     
  .      ,
   (  ) 
    .
      ,   ,   ,
 ,  , .  ,   ,
 -  iStack,    4.15, 
        .   iStack
  :

   #include <vector>

   class iStack {
   public:
      iStack( int capacity )
         : _stack( capacity ), _top( 0 ) { }

      bool pop( int &top_value );
      bool push( int value );
      bool full();
      bool empty();
      void display();

      int size();

   private:
      int _top;
      vector< int > _stack;
   };

                                                                  ++   530

           int.    
iStack      int,   ( 
,   )     . ,
    myStack,      20
  int:

   iStack myStack(20);

       myStack    :

     pop(),   ;
     push(),   .

           . 
  ?
   -,   ,      . 
C++       .     
    13,        , 
      iStack.     
  stackExcp.h:

   // stackExcp.h
   class popOnEmpty { /* ... */ };
   class pushOnFull { /* ... */ };

     19       ,  
   ,   
C++.
       - pop()  push() ,  
  .     throw,  
   return.      throw, 
     ,     . 
  throw   pop()?   :

   // ,    
   throw popOnEmpty;

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

   //    
   throw popOnEmpty();

         popOnEmpty.

                                                            ++   531

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

   class iStack {
   public:
      // ...

      //    
      void pop( int &value );
      void push( int value );
   private:
      // ...
   };

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

   #include "stackExcp.h"

   void iStack::pop( int &top_value )
   {
      if ( empty() )
         throw popOnEmpty();

      top_value = _stack[ --_top ];

      cout << "iStack::pop(): " << top_value << endl;
   }

   void iStack::push( int value )
   {
      cout << "iStack::push( " << value << " )\n";

      if ( full() )
         throw pushOnFull( value );

      _stack[ _top++ ] = value;
   }

           , 
throw     . ,  mathFunc() 
      - . 
  C++:

                                                           ++   532

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

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

                                 11.1

       throw ? ?  
    :

   (a) class exceptionType { };
       throw exceptionType();
   (b) int excpObj;
       throw excpObj;
   (c) enum mathErr { overflow, underflow, zeroDivide };
       throw mathErr zeroDivide();
   (d) int *pi = excpObj;
       throw pi;

                               11.2

     IntArray,    2.3,  -
operator[](),    assert()    ,   
  .     ,   
   .  ,   
   .

                                11.2. try-

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

                                                              ++   533

   #include <iostream>
   #include "iStack.h"

   int main() {
      iStack stack( 32 );

      stack.display();
      for ( int ix = 1; ix < 51; ++ix )
      {
         if ( ix % 3 == 0 )
            stack.push( ix );

         if ( ix % 4 == 0 )
            stack.display();

         if ( ix % 10 == 0 ) {
            int dummy;
            stack.pop( dummy );
            stack.display();
         }
      }
      return 0;
   }

   ,    ,     try-.
      try,    
,    ,      ,
 catch-. Try-    
    .    try- 
 main(),     popOnEmpty  pushOnFull?

   for ( int ix = 1; ix < 51; ++ix ) {
      try { // try-   pushOnFull
         if ( ix % 3 == 0 )
            stack.push( ix );
      }
      catch ( pusOnFull ) { ... }

      if ( ix % 4 == 0 )
         stack.display();

      try { // try-   popOnEmpty
         if ( ix % 10 == 0 ) {
            int dummy;
            stack.pop( dummy );
            stack.display();
      }
   }
   catch ( popOnEmpty ) { ... }
   }

        .     
  ,    ,  
 .   ,     ,
    .     

                                                             ++   534

  ,    .  ,   
     :

   try {
     for ( int ix = 1; ix < 51; ++ix )
     {
        if ( ix % 3 == 0 )
           stack.push( ix );

        if ( ix % 4 == 0 )
           stack.display();

        if ( ix % 10 == 0 ) {
           int dummy;
           stack.pop( dummy );
           stack.display();
        }
      }
   }
   catch ( pushOnFull ) { ... }
   catch ( popOnEmpty ) { ... }

    try-   catch-,   
 pushOnFull  popOnEmpty,  - push() 
pop()   .  catch-   
.        
(  ),    catch-. (
catch-     .)
           :

      ,     try-, 
        .  main()  0;
    - push(),     if   for,
     ,      if ,
       for  try-,    
     pushOnFull;
    - pop(),     if   for,
     ,   display() ,  
     for  try-,      popOnEmpty.

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

                                                               ++   535

   int main() {
      try {
         iStack stack( 32 ); // :   try-

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

      //     stack  
      return 0;
  }

      ,        try-.   
  try-   ,     
 try-.      
        . :

   int main()
      try {
         iStack stack( 32 ); // :   try-

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

    ,    try    ,
  ,  catch-    
.  , ,   ,   
        .   ,
  main(),     .
    try-   catch-   . 
  ,   ,   
,   ,     .  try-
      . (     
  19.)

                                                                  ++   536

                             11.3

    ,    IntArray (  IntArray
   2.3)     .
      ,   .

  1.        IntArray , , , ..., n-
      ( n ).     IntArray.
  2.        IntArray , , ..., n-
      ( n  5).   .
  3.        IntArray , , ..., n-
      ( n ).   .

     operator[]()  IntArray,  
 11.2,        IntArray.  
operator[]()   ,  ,  
 try-  catch-. ,    try-
 ,   .

                            11.3.  

     C++     catch.  -
  try-  ,   
  catch  ,    .
   Catch-    :   catch,  
   ,     (  
),   .     
 catch-,     . 
catch-  pushOnFull  popOnEmpty   main() 
:

   catch ( pushOnFull ) {
      cerr << "trying to push value on a full stack\n";
      return errorCode88;
   }
   catch ( popOnEmpty ) {
      cerr << "trying to pop a value on an empty stack\n";
      return errorCode89;
   }

     catch-    ;    pushOnFull,  
  popOnEmpty.      , 
         . (
 19  ,      :   
       .) , 
- pop()  iStack   popOnEmpty,  
   .       cerr, 
main()   errorCode89.
     catch-    return,    
  ?    

                                                               ++   537

  ,    catch-  . 
      return   main().  
 catch- popOnEmpty    , main()  0.

   int main() {
      iStack stack( 32 );

      try {
         stack.display();
         for ( int x = 1; ix < 51; ++ix )
         {
            //  ,   
         }
      }
      catch ( pushOnFull ) {
         cerr << "trying to push value on a full stack\n";
      }
      catch ( popOnEmpty ) {
         cerr << "trying to pop a value on an empty stack\n";
      }

      //   
      return 0;
   }

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

                      11.3.1. -

      catch-      .
     ? ,      -
  ,    throw.   
 ,   -   
         , 
  catch-    ,  
  throw.
       pushOnFull,   -
 ,      . Catch-,   ,
     cerr.      
  pushOnFull  :

   //   :
   //   ,      
   class pushOnFull {
   public:
      pushOnFull( int i ) : _value( i ) { }
      int value { return _value; }
   private:
      int _value;

                                                        ++   538

   };

      _value  ,      .
    int      _data.  
        throw:

   void iStack::push( int value )
   {
      if ( full() )
         // ,   -
         throw pushOnFull( value );

         // ...
   }

     pushOnFull    - value(),  
  catch-     -
:

   catch ( pushOnFull eObj ) {
      cerr << "trying to push value " << eObj.value()
           << " on a full stack\n";
   }

    ,      catch- 
 eObj,     - value() 
pushOnFull.
   -     ,    throw 
    ,   ,    . :

   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-  .
         ,    . 
   catch-  ,     ,  
  -. ,  

                                                             ++   539

calculate()    mathFunc().    catch-
 calculate()  eObj   -,
  throw.

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

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

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

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

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

                                                            ++   540

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

                       11.3.2.  

    catch-    
.   throw   try-,    
 catch     ,    
.    catch ,  
.        .
,   ,     
,   try-;       catch,
   .       , 
 .        
 .       
 .      , 
   .
       ,    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 -
  .      
  ,  ,    ,  
 ,      .    C++
     . ,   

                                                          ++   541

      ,   
  .       ,  
  catch-     .
 terminate()    ,  
  ,     .

                       11.3.3.   

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

   throw;

   -.    
  ,   catch-:

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

       -  .   ,
 catch-  ,    
.     -  . ?

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

   void calculate( int op ) {
      try {
         // ,  mathFunc(),   zeroOp
         mathFunc( op );
      }
      catch ( EHstate eObj ) {
        // - 
        //   -
        eObj = severeErr;
        // ,     
        //   severeErr
        throw;
      }
   }

                                                            ++   542

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

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

      //      severeErr
      throw;
   }

    eObj   -,   throw,   
     .   
     .
    ,       catch- 
 ,      -  
      . (  
   19.2,   ,  catch- 
  .)

                   11.3.4.   

          ,   
 ,    ,      . 
,    ,     
  ,       :

   void manip() {
      resource res;
      res.lock(); //  

      //  
      // ,     

      res.release(); //  ,   
   }

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

                                                          ++   543

   //       
   catch (...) {
      //    
   }

    catch(...)      
.        catch-
  ,        
  :

   void manip() {
      resource res;
      res.lock();

      try {
         //  
         // ,     
      }
      catch (...) {
         res.release();
         throw;
      }
      res.release(); //  ,   
   }

        ,    manip()
   ,     catch(...)  ,
    .     
         .  
   ,      
. (       19.)
    catch(...)        catch-
.        
,   try-.
   Catch-   ,   ,    . 
  ,  . ,  
catch(...)     catch-,    
  ,      :

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

                                     11.4

                                                             ++   544

   ,      C++  .

                                 11.5

      .   throw, 
-,      :

   (a) class exceptionType { };
       catch( exceptionType *pet ) { }
   (b) catch(...) { }
   (c) enum mathErr { overflow, underflow, zeroDivide };
       catch( mathErr &ref ) { }
   (d) typedef int EXCPTYPE;
       catch( EXCPTYPE ) { }

                                 11.6

   ,      .

                                 11.7

     ,       catch 
 .

                                 11.8

     ,     11.3,   
:  ,    operator[](), 
  -    catch-. 
 ,  operator[]()     .

                        11.4.  

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

   class iStack {
   public:
      // ...

      void pop( int &value ); //  popOnEmpty
      void push( int value ); //  pushOnFull

   private:
      // ...
   };

      . ,     
   iStack.  ,    
   ,       .
       

                                                            ++   545

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

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

   ,     pop()     ,
 popOnEmpty,     push() pushOnFull.
         ,      
   .      
      ,  ,  
    ,  .
         ,   
          .
             
:

   //       
   extern int foo( int = 0 ) throw(string);

   // :   
   extern int foo( int parm ) { }

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

                                                           ++   546

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

    recoup()    string,     
.       , unexpected() 
.
          
.     ,    throw 
  .        
 ,  ,    ,  
,     :

   extern void doit( int, int ) throw(string, exceptionType);
   void action ( int op1, int op2 ) throw(string) {
      doit( op1, op2 ); //    
      // ...
   }

   doit()     exceptionType,   
 action().    .  
  , ,    , 
,     unexpected().
     ,      :

   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 (*pf)( int ) throw(string);

      ,  pf   ,  
    string.     ,
          
,    :

   extern void (*pf) ( int ) throw(string);
   // :   
   void (*pf)( int );

              
 ,         
.       
.   -     
   ,     ( , 
 ). :

   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;

                                                      ++   548

       .   ,  pf3
 ,       string. 
doit()     exceptionType.   
  ,    pf3,   
    pf3,     .

                               11.9

    ,    11.8,   
operator[]()   IntArray,    
.   ,  operator[]() 
,    .    ?

                              11.10

       ,     
 throw()?       ?

                            11.11

       ? ?

   void example() throw(string);
   (a) void (*pf1)() = example;
   (b) void (*pf2) throw() = example;

                       11.5.    

        C++   . 
     ,     .  
        
 . ,      
    .   
  ,     ,
       .
          iStack   -. 
,  ,  main(),   
,   . -  iStack  , 
 pop() ,   ,    push() ,  
;        ,  
,       .    
 -,    ,  
 .
    C++  ,      
 (,   )  ,    . 
  :      ?  .
    ,    , 
 .      ,    , 
  ,     .  
    ,    

                                                             ++   549

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

   void iStack::push( int value )
   {
      //   ,   
      if ( full() )
         _stack.resize( 2 * _stack.size() );
      _stack[ _top++ ] = value;
   }

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

                                                              ++   550

         .  try- 
   catch-   , 
   . Catch-  ,  
 ,        .  
    (.  11.4).
         ,  ,
  19,       .
