]> Creatis software - gdcm.git/blob - src/gdcmException.h
added exception classes
[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 #include <iostream>
20 #include <exception>
21 using namespace std;
22
23 #ifdef _MSC_VER
24 #define GDCM_EXPORT __declspec( dllexport )
25 #else
26 #define GDCM_EXPORT
27 #endif
28
29 /**
30  * Any exception thrown in the gdcm library
31  */
32 class GDCM_EXPORT gdcmException : public exception {
33  protected:
34   /// error message
35   string from;
36   /// error message
37   string error;
38
39   /// exception caught within exception class: print error message and die
40   static void fatal(const char *from) throw();
41
42   /// try to discover this (dynamic) class name
43   virtual string getName() const throw();
44
45  public:
46   /**
47    * Builds an exception with minimal information: name of the thrower
48    * method and error message
49    *
50    * @param from name of the thrower
51    * @param error error description string
52    */
53   explicit gdcmException(const string &from, const string &error = "")
54     throw();
55   
56
57   /**
58    * virtual descructor makes this class dynamic
59    */
60   virtual ~gdcmException() {
61   }
62   
63   /// returns error message
64   const string &getError(void) const throw() {
65     return error;
66   }
67
68   /// returns exception name string
69   operator const char *() const throw();
70
71   /// returns exception name string (overloads std::exception::what)
72   virtual const char *what() const throw() {
73     return (const char *) *this;
74   }
75
76
77   friend ostream& operator<<(ostream &os, const gdcmException &e);
78   
79 };
80
81
82 /** prints exception stack on output stream
83  * @param os output stream
84  * @param e exception to print
85  * @returns output stream os
86  */
87 ostream& operator<<(ostream &os, const gdcmException &e);
88
89
90 /**
91  * File error exception thrown in the gdcm library
92  */
93 class GDCM_EXPORT gdcmFileError : public gdcmException {
94  public:
95   /**
96    * Builds an file-related exception with minimal information: name of
97    * the thrower method and error message
98    *
99    * @param from name of the thrower
100    * @param error error description string
101    */
102   explicit gdcmFileError(const string &from,
103                          const string &error = "File error")
104     throw() : gdcmException(from, error) {
105   }
106 };
107
108
109
110
111 /**
112  * Invalid file format exception
113  */
114 class GDCM_EXPORT gdcmFormatError : public gdcmException {
115  public:
116   /**
117    * Builds an file-related exception with minimal information: name of
118    * the thrower method and error message
119    *
120    * @param from name of the thrower
121    * @param error error description string
122    */
123   explicit gdcmFormatError(const string &from,
124                            const string &error = "Invalid file format error")
125     throw() : gdcmException(from, error) {
126   }
127 };
128
129
130 #endif // GDCM_EXCEPTION_H