]> Creatis software - gdcm.git/blob - src/gdcmException.h
#include <string>
[gdcm.git] / src / gdcmException.h
1 // gdcm.h
2
3 // gdcmlib Intro:  
4 // * gdcmlib is a library dedicated to reading and writing dicom files.
5 // * LGPL for the license
6 // * lightweigth as opposed to CTN or DCMTK which come bundled which try
7 //   to implement the full DICOM standard (networking...). gdcmlib concentrates
8 //   on reading and writing
9 // * Formats: this lib should be able to read ACR-NEMA v1 and v2, Dicom v3 (as
10 //   stated in part10). [cf dcmtk/dcmdata/docs/datadict.txt]
11 // * Targeted plateforms: Un*xes and Win32/VC++6.0
12 //
13 //
14
15 #ifndef GDCM_EXCEPTION_H
16 #define GDCM_EXCEPTION_H
17
18 #include <string>
19 #ifdef _MSC_VER
20 using namespace std;  // string type lives in the std namespace on VC++
21 #endif
22
23 #include <iostream>
24 #include <exception>
25 using namespace std;
26
27 #ifdef _MSC_VER
28 #define GDCM_EXPORT __declspec( dllexport )
29 #else
30 #define GDCM_EXPORT
31 #endif
32
33 /**
34  * Any exception thrown in the gdcm library
35  */
36 class GDCM_EXPORT gdcmException : public exception {
37  protected:
38   /// error message
39   string from;
40   /// error message
41   string error;
42
43  public:
44   /**
45    * Builds an exception with minimal information: name of the thrower
46    * method and error message
47    *
48    * @param from name of the thrower
49    * @param error error description string
50    */
51   explicit gdcmException(const string &from, const string &error = "")
52     throw();
53   
54
55   /**
56    * virtual destructor makes this class dynamic
57    */
58   virtual ~gdcmException() {
59   }
60   
61   /// returns error message
62   const string &getError(void) const throw() {
63     return error;
64   }
65
66   /// returns exception name string
67   operator const char *() const throw();
68
69   /// returns exception name string (overloads std::exception::what)
70   virtual const char *what() const throw() {
71     return (const char *) *this;
72   }
73
74
75   /// exception caught within exception class: print error message and die
76   static void fatal(const char *from) throw();
77
78   /// try to discover this (dynamic) class name
79   virtual string getName() const throw();
80
81   friend ostream& operator<<(ostream &os, const gdcmException &e);
82   
83 };
84
85
86 /** prints exception stack on output stream
87  * @param os output stream
88  * @param e exception to print
89  * @returns output stream os
90  */
91 ostream& operator<<(ostream &os, const gdcmException &e);
92
93
94 /**
95  * File error exception thrown in the gdcm library
96  */
97 class GDCM_EXPORT gdcmFileError : public gdcmException {
98  public:
99   /**
100    * Builds an file-related exception with minimal information: name of
101    * the thrower method and error message
102    *
103    * @param from name of the thrower
104    * @param error error description string
105    */
106   explicit gdcmFileError(const string &from,
107                          const string &error = "File error")
108     throw() : gdcmException(from, error) {
109   }
110 };
111
112
113
114
115 /**
116  * Invalid file format exception
117  */
118 class GDCM_EXPORT gdcmFormatError : public gdcmException {
119  public:
120   /**
121    * Builds an file-related exception with minimal information: name of
122    * the thrower method and error message
123    *
124    * @param from name of the thrower
125    * @param error error description string
126    */
127   explicit gdcmFormatError(const string &from,
128                            const string &error = "Invalid file format error")
129     throw() : gdcmException(from, error) {
130   }
131 };
132
133
134 #endif // GDCM_EXCEPTION_H