]> Creatis software - gdcm.git/blob - src/gdcmException.h
* src/*.[h|cxx] : coding style
[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     * 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
56    std::string from;
57    // error message
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     * Builds an file-related exception with minimal information: name of
70     * the thrower method and error message
71     *
72     * @param from name of the thrower
73     * @param error error description string
74     */
75    explicit gdcmFileError(const std::string &from,
76                           const std::string &error = "File error")
77       throw() : gdcmException(from, error) {
78    }
79 };
80
81
82 //-----------------------------------------------------------------------------
83 /*
84  * Invalid file format exception
85  */
86 class GDCM_EXPORT gdcmFormatError : public gdcmException {
87 public:
88    /*
89     * Builds an file-related exception with minimal information: name of
90     * the thrower method and error message
91     *
92     * @param from name of the thrower
93     * @param error error description string
94     */
95    explicit gdcmFormatError(const std::string &from,
96                             const std::string &error = "Invalid file format error")
97       throw() : gdcmException(from, error) {
98    }
99 };
100
101 //-----------------------------------------------------------------------------
102 /* prints exception stack on output stream
103  * @param os output stream
104  * @param e exception to print
105  * @returns output stream os
106  */
107 std::ostream& operator<<(std::ostream &os, const gdcmException &e);
108
109 //-----------------------------------------------------------------------------
110 #endif // GDCM_EXCEPTION_H