00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmException.h"
00020
00021 #include <typeinfo>
00022 namespace gdcm
00023 {
00024
00025
00031 Exception::Exception(const std::string &f, const std::string &msg) throw()
00032 #ifdef __GNUC__
00033 try
00034 #endif
00035 : From(f), Error(msg)
00036 {
00037 }
00038 #ifdef __GNUC__
00039 catch(...)
00040 {
00041 fatal("Exception::Exception(const std::string&, const std::string&, const std::string&)");
00042 }
00043 #endif
00044
00049 void Exception::fatal(const char *from) throw()
00050 {
00051 try
00052 {
00053 std::cerr << "Fatal: exception received in " << from
00054 << " while handling exception." << std::endl;
00055 exit(-1);
00056 }
00057 catch(...)
00058 {
00059 try
00060 {
00061 std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
00062 << std::endl;
00063 exit(-1);
00064 }
00065 catch(...)
00066 {
00067 exit(-1);
00068 }
00069 }
00070 }
00071
00076 std::string Exception::getName() const throw()
00077 {
00078 try
00079 {
00080 #if defined(__GNUC__) && 0 // GNU C++ compiler class name demangling
00081 unsigned int nested = 1, i, nb;
00082 int offset;
00083 std::string one;
00084
00085 std::string name;
00086 std::string iname = typeid(*this).name();
00087 if(iname[0] == 'Q')
00088 {
00089 nested = iname[1] - '0';
00090 iname = std::string(iname, 2, std::string::npos);
00091 }
00092 for(i = 0; i < nested; i++)
00093 {
00094 ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
00095 iname = std::string(iname, offset, std::string::npos);
00096 name += std::string(iname, 0, nb);
00097 if(i + 1 < nested) name += "::";
00098 iname = std::string(iname, nb, std::string::npos);
00099 }
00100 return name;
00101 #else // no class name demangling
00102 std::string name = typeid(*this).name();
00103 return name;
00104 #endif
00105 }
00106 catch(...)
00107 {
00108 fatal("Exception::getName(std::string &)");
00109 return "";
00110 }
00111 }
00112
00116 Exception::operator const char *() const throw()
00117 {
00118 return getName().c_str();
00119 }
00120
00121
00127 std::ostream& operator<<(std::ostream &os, const Exception &e)
00128 {
00129 try
00130 {
00131 os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
00132 }
00133 catch(...)
00134 {
00135 Exception::fatal("operator<<(std::ostream &, const Exception&)");
00136 }
00137 return os;
00138 }
00139
00140 }
00141