]> Creatis software - gdcm.git/blob - src/gdcmException.h
BUG: Remove demangle code this was seg faulting on some linux gcc 3.3.2 machine
[gdcm.git] / src / gdcmException.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.h,v $
5   Language:  C++
6   Date:      $Date: 2004/11/04 18:14:34 $
7   Version:   $Revision: 1.18 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #ifndef GDCM_EXCEPTION_H
20 #define GDCM_EXCEPTION_H
21
22 #include "gdcmCommon.h"
23 #include <string>
24 #include <iostream>
25 #include <exception>
26 namespace gdcm 
27 {
28
29 //-----------------------------------------------------------------------------
30 /*
31  * Any exception thrown in the gdcm library
32  */
33 class GDCM_EXPORT Exception : public std::exception
34 {
35 public:
36    /*
37     * Builds an exception with minimal information: name of the thrower
38     * method and error message
39     *
40     * @param from name of the thrower
41     * @param error error description string
42     */
43    explicit Exception(const std::string &from, const std::string &error = "")
44       throw();
45
46    /**
47     * \brief virtual destructor makes this class dynamic
48     */
49    virtual ~Exception() throw() {};
50
51    /// exception caught within exception class: print error message and die
52    static void fatal(const char *from) throw();
53
54    /// returns error message
55    const std::string &getError() const throw() {
56       return Error;
57    }
58
59    /// try to discover this (dynamic) class name
60    virtual std::string getName() const throw();
61
62    /// returns exception name string (overloads std::exception::what)
63    virtual const char *what() const throw() {
64       return (const char *) *this;
65    }
66
67    /// returns exception name string
68    operator const char *() const throw();
69
70    friend std::ostream& operator<<(std::ostream &os, const Exception &e);
71
72 protected:
73    /// error message part 1
74    std::string From;
75    /// error message part 2
76    std::string Error;
77 };
78
79
80 //-----------------------------------------------------------------------------
81 /*
82  * File error exception thrown in the gdcm library
83  */
84 class GDCM_EXPORT FileError : public Exception
85 {
86 public:
87    /**
88     * \brief Builds an file-related exception with minimal information: name of
89     *               the thrower method and error message
90     * @param from name of the thrower
91     * @param error error description string
92     */
93    explicit FileError(const std::string &from,
94                           const std::string &error = "File error")
95       throw() : Exception(from, error) { }
96 };
97
98
99 //-----------------------------------------------------------------------------
100 /**
101  * \brief Unexpected file format exception
102  */
103 class GDCM_EXPORT FormatUnexpected : public Exception
104 {
105 public:
106    /// \brief Builds a file-related exception with minimal information:
107    /// name of the thrower method and error message
108    /// @param from name of the thrower
109    /// @param error error description string
110    explicit FormatUnexpected(const std::string &from,
111                              const std::string &error = "Unexpected file format")
112       throw() : Exception( from, error ) { }
113 };
114
115 //-----------------------------------------------------------------------------
116 /**
117  * \brief Invalid file format exception
118  */
119 class GDCM_EXPORT FormatError : public FormatUnexpected
120 {
121 public:
122    /// \brief Builds a file-related exception with minimal information:
123    /// name of the thrower method and error message
124    /// @param from name of the thrower
125    /// @param error error description string
126    explicit FormatError(const std::string &from,
127                         const std::string &error = "Invalid file format")
128       throw() : FormatUnexpected( from, error ) { }
129 };
130
131 //-----------------------------------------------------------------------------
132 /* prints exception stack on output stream
133  * @param os output stream
134  * @param e exception to print
135  * @returns output stream os
136  */
137 std::ostream& operator<<(std::ostream &os, const Exception &e);
138
139 } // end namespace gdcm
140
141 //-----------------------------------------------------------------------------
142 #endif // GDCM_EXCEPTION_H