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