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 #include <stdlib.h>
00023
00024 namespace GDCM_NAME_SPACE
00025 {
00026
00027
00033 Exception::Exception(const std::string &f, const std::string &msg) throw()
00034 #ifdef __GNUC__
00035 try
00036 #endif
00037 : From(f), Error(msg)
00038 {
00039 }
00040 #ifdef __GNUC__
00041 catch(...)
00042 {
00043 fatal("Exception::Exception(const std::string&, const std::string&, const std::string&)");
00044 }
00045 #endif
00046
00051 void Exception::fatal(const char *from) throw()
00052 {
00053 try
00054 {
00055 std::cerr << "Fatal: exception received in " << from
00056 << " while handling exception." << std::endl;
00057 exit(-1);
00058 }
00059 catch(...)
00060 {
00061 try
00062 {
00063 std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
00064 << std::endl;
00065 exit(-1);
00066 }
00067 catch(...)
00068 {
00069 exit(-1);
00070 }
00071 }
00072 }
00073
00078 std::string Exception::getName() const throw()
00079 {
00080 try
00081 {
00082 #if defined(__GNUC__) && 0 // GNU C++ compiler class name demangling
00083 unsigned int nested = 1, i, nb;
00084 int offset;
00085 std::string one;
00086
00087 std::string name;
00088 std::string iname = typeid(*this).name();
00089 if ( iname[0] == 'Q' )
00090 {
00091 nested = iname[1] - '0';
00092 iname = std::string(iname, 2, std::string::npos);
00093 }
00094 for(i = 0; i < nested; i++)
00095 {
00096 ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
00097 iname = std::string(iname, offset, std::string::npos);
00098 name += std::string(iname, 0, nb);
00099 if ( i + 1 < nested) name += "::";
00100 iname = std::string(iname, nb, std::string::npos );
00101 }
00102 return name;
00103 #else // no class name demangling
00104 std::string name = typeid(*this).name();
00105 return name;
00106 #endif
00107 }
00108 catch(...)
00109 {
00110 fatal("Exception::getName(std::string &)");
00111 return "";
00112 }
00113 }
00114
00118 Exception::operator const char *() const throw()
00119 {
00120 return getName().c_str();
00121 }
00122
00123
00129 std::ostream& operator<<(std::ostream &os, const Exception &e)
00130 {
00131 try
00132 {
00133 os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
00134 }
00135 catch(...)
00136 {
00137 Exception::fatal("operator<<(std::ostream &, const Exception&)");
00138 }
00139 return os;
00140 }
00141
00142 }
00143