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