]> Creatis software - gdcm.git/blob - src/gdcmException.cxx
Doxygenation (Print method)
[gdcm.git] / src / gdcmException.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 14:28:32 $
7   Version:   $Revision: 1.25 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmException.h"
20
21 #include <typeinfo>
22 namespace gdcm 
23 {
24
25 //-----------------------------------------------------------------------------
26 // Exception
27
28 /**
29  * \brief constructor
30  * @param f f
31  * @param msg msg 
32  */
33 Exception::Exception(const std::string &f, const std::string &msg) throw()
34 #ifdef __GNUC__
35   try
36 #endif
37   : From(f), Error(msg) {
38   }
39 #ifdef __GNUC__
40 catch(...) {
41   fatal("Exception::Exception(const std::string&, const std::string&, const std::string&)");
42 }
43 #endif
44
45
46 /**
47  * \brief fatal
48  * @param from from
49  */
50 void Exception::fatal(const char *from) throw() {
51    try
52    {
53       std::cerr << "Fatal: exception received in " << from 
54                 << " while handling exception." << std::endl;
55       exit(-1);
56    }
57    catch(...)
58    {
59       try
60       {
61          std::cerr << "Fatal: exception received in Exception::fatal while handling exception."
62                    << std::endl;
63          exit(-1);
64       }
65       catch(...)
66       {
67          exit(-1);
68       }
69    }
70 }
71
72 /**
73  * \brief getName
74  * @return string
75  */
76 std::string Exception::getName() const throw()
77 {
78    try
79    {
80 #if defined(__GNUC__) && 0   // GNU C++ compiler class name demangling
81       unsigned int nested = 1, i, nb;
82       int offset;
83       std::string one;
84
85       std::string name;
86       std::string iname = typeid(*this).name();
87       if(iname[0] == 'Q')
88       {
89          nested = iname[1] - '0';
90          iname = std::string(iname, 2, std::string::npos);
91       }
92       for(i = 0; i < nested; i++)
93       {
94          ::sscanf(iname.c_str(), "%u%n", &nb, &offset);
95          iname = std::string(iname, offset, std::string::npos);
96          name += std::string(iname, 0, nb);
97          if(i + 1 < nested) name += "::";
98          iname = std::string(iname, nb, std::string::npos);
99       }
100       return name;
101 #else           // no class name demangling
102       std::string name = typeid(*this).name();
103       return name;
104 #endif
105   }
106   catch(...) {
107     fatal("Exception::getName(std::string &)");
108     return "";
109   }
110 }
111
112 /**
113  * \brief Exception
114  */
115  Exception::operator const char *() const throw() {
116   return getName().c_str();
117 }
118
119 //-----------------------------------------------------------------------------
120 /**
121  * \brief Exception::operator <<
122  * @param os ostream to write to
123  * @param e exception to raise
124  */
125  std::ostream& operator<<(std::ostream &os, const Exception &e) {
126   try {  
127     os << "Exception " << e.getName() << " thrown: " << e.getError() << std::endl;
128   }
129   catch(...) {
130     Exception::fatal("operator<<(std::ostream &, const Exception&)");
131   }
132   return os;
133 }
134 } // end namespace gdcm
135 //-----------------------------------------------------------------------------