]> Creatis software - gdcm.git/blob - src/gdcmException.h
* gdcmPython/Makefile.am now avoids calling the wrappers for the
[gdcm.git] / src / gdcmException.h
1 #ifndef GDCM_EXCEPTION_H
2 #define GDCM_EXCEPTION_H
3
4 #include <string>
5 #include <iostream>
6 #include <exception>
7 #include "gdcmCommon.h"
8
9 /**
10  * Any exception thrown in the gdcm library
11  */
12 class GDCM_EXPORT gdcmException : public std::exception {
13  protected:
14   /// error message
15   std::string from;
16   /// error message
17   std::string error;
18
19  public:
20   /**
21    * Builds an exception with minimal information: name of the thrower
22    * method and error message
23    *
24    * @param from name of the thrower
25    * @param error error description string
26    */
27   explicit gdcmException(const std::string &from, const std::string &error = "")
28     throw();
29   
30
31   /**
32    * virtual destructor makes this class dynamic
33    */
34   virtual ~gdcmException() throw() {
35   }
36   
37   /// returns error message
38   const std::string &getError(void) const throw() {
39     return error;
40   }
41
42   /// returns exception name string
43   operator const char *() const throw();
44
45   /// returns exception name string (overloads std::exception::what)
46   virtual const char *what() const throw() {
47     return (const char *) *this;
48   }
49
50
51   /// exception caught within exception class: print error message and die
52   static void fatal(const char *from) throw();
53
54   /// try to discover this (dynamic) class name
55   virtual std::string getName() const throw();
56
57   friend std::ostream& operator<<(std::ostream &os, const gdcmException &e);
58   
59 };
60
61
62 /** prints exception stack on output stream
63  * @param os output stream
64  * @param e exception to print
65  * @returns output stream os
66  */
67 std::ostream& operator<<(std::ostream &os, const gdcmException &e);
68
69
70 /**
71  * File error exception thrown in the gdcm library
72  */
73 class GDCM_EXPORT gdcmFileError : public gdcmException {
74  public:
75   /**
76    * Builds an file-related exception with minimal information: name of
77    * the thrower method and error message
78    *
79    * @param from name of the thrower
80    * @param error error description string
81    */
82   explicit gdcmFileError(const std::string &from,
83                          const std::string &error = "File error")
84     throw() : gdcmException(from, error) {
85   }
86 };
87
88
89 /**
90  * Invalid file format exception
91  */
92 class GDCM_EXPORT gdcmFormatError : public gdcmException {
93  public:
94   /**
95    * Builds an file-related exception with minimal information: name of
96    * the thrower method and error message
97    *
98    * @param from name of the thrower
99    * @param error error description string
100    */
101   explicit gdcmFormatError(const std::string &from,
102                            const std::string &error = "Invalid file format error")
103     throw() : gdcmException(from, error) {
104   }
105 };
106
107
108 #endif // GDCM_EXCEPTION_H