]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
added exception classes
[gdcm.git] / src / gdcmException.cxx
1 #include "gdcmException.h"
2
3 #include <typeinfo>
4 #include <stdio.h>
5
6
7 gdcmException::gdcmException(const string &f, const string& msg) throw()
8 #ifdef __GNUC__
9   try
10 #endif
11   : from(f), error(msg) {
12   }
13 #ifdef __GNUC__
14 catch(...) {
15   fatal("gdcmException::gdcmException(const std::string&, const std::string&, const std::string&)");
16 }
17 #endif
18
19
20 void gdcmException::fatal(const char *from) throw() {
21   try {
22     cerr << "Fatal: exception received in " << from 
23          << " while handling exception." << endl;
24     exit(-1);
25   }
26   catch(...) {
27     try {
28       cerr << "Fatal: exception received in Exception::fatal while handling exception."
29            << endl;
30       exit(-1);
31     }
32     catch(...) {
33       exit(-1);
34     }
35   }  
36 }
37
38
39 string gdcmException::getName() const throw() {
40   try {
41 #ifdef __GNUC__   // GNU C++ compiler class name demangling
42       unsigned int nested = 1, i, nb, offset;
43       string one;
44
45       string name;
46       string iname = typeid(*this).name();
47       if(iname[0] == 'Q') {
48         nested = iname[1] - '0';
49         iname = string(iname, 2, std::string::npos);
50       }
51       for(i = 0; i < nested; i++) {
52         ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
53         iname = string(iname, offset, std::string::npos);
54         name += string(iname, 0, nb);
55         if(i + 1 < nested) name += "::";
56         iname = string(iname, nb, std::string::npos);
57       }
58       return name;
59 #else             // no class name demangling
60       //name = typeid(*this).name();
61       return "Exception";
62 #endif
63   }
64   catch(...) {
65     fatal("Exception::getName(string &)");
66     return "";
67   }
68 }
69
70
71 gdcmException::operator const char *() const throw() {
72   return getName().c_str();
73 }
74
75
76 ostream& operator<<(ostream &os, const gdcmException &e) {
77   try {  
78     os << "Exception " << e.getName() << " thrown: " << e.error << endl;
79   }
80   catch(...) {
81     gdcmException::fatal("operator<<(ostream &, const gdcmException&)");
82   }
83   return os;
84 }
85
86