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