src/work/klao/error2_test.cc
changeset 1365 c280de819a73
equal deleted inserted replaced
0:ab9298b3b2af -1:000000000000
     1 #include <iostream>
       
     2 #include <memory>
       
     3 
       
     4 #include <boost/shared_ptr.hpp>
       
     5 
       
     6 using namespace std;
       
     7 
       
     8 class Ex : public exception {
       
     9 
       
    10   typedef exception Parent;
       
    11 
       
    12   mutable
       
    13   auto_ptr<string> uz;
       
    14 public:
       
    15 
       
    16   // boost::shared_ptr<string> uz;
       
    17 
       
    18   Ex(const Ex &e) : Parent(e), uz(e.uz) {}
       
    19 
       
    20   explicit
       
    21   Ex(const char *msg = 0) {
       
    22     if( msg ) {
       
    23       try {
       
    24 	uz.reset(new string);
       
    25 	*uz = msg;
       
    26       }
       
    27       catch(...) {
       
    28 	uz.reset();
       
    29       }
       
    30     }
       
    31   }
       
    32 
       
    33   virtual
       
    34   const char* what() const throw() {
       
    35     if( uz.get() )
       
    36       return uz->c_str();
       
    37     return "Kivetel";
       
    38   }
       
    39 
       
    40   virtual ~Ex() throw() {}
       
    41 };
       
    42 
       
    43 static void fn1() {
       
    44   Ex e("alma");
       
    45   throw e;
       
    46 }
       
    47 
       
    48 static
       
    49 void fn2() {
       
    50   throw Ex("korte");
       
    51 }
       
    52 
       
    53 int main() {
       
    54   try {
       
    55     fn1();
       
    56   }
       
    57   catch(exception const &e) {
       
    58     cerr << "Hiba: " << e.what() << endl;
       
    59   }
       
    60 
       
    61   try {
       
    62     fn2();
       
    63   }
       
    64   catch(exception const &e) {
       
    65     cerr << "Hiba: " << e.what() << endl;
       
    66   }
       
    67 
       
    68 }