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