]> Creatis software - gdcm.git/blob - src/gdcmException.h
* src/*.cxx *.h Reference to License.htm fixed to License.html.
[gdcm.git] / src / gdcmException.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.h,v $
5   Language:  C++
6   Date:      $Date: 2004/09/27 08:39:07 $
7   Version:   $Revision: 1.16 $
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
27 //-----------------------------------------------------------------------------
28 /*
29  * Any exception thrown in the gdcm library
30  */
31 class GDCM_EXPORT gdcmException : public std::exception
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     * \brief virtual destructor makes this class dynamic
46     */
47    virtual ~gdcmException() throw() {};
48
49    /// exception caught within exception class: print error message and die
50    static void fatal(const char *from) throw();
51
52    /// returns error message
53    const std::string &getError() const throw() {
54       return Error;
55    }
56
57    /// try to discover this (dynamic) class name
58    virtual std::string getName() const throw();
59
60    /// returns exception name string (overloads std::exception::what)
61    virtual const char *what() const throw() {
62       return (const char *) *this;
63    }
64
65    /// returns exception name string
66    operator const char *() const throw();
67
68    friend std::ostream& operator<<(std::ostream &os, const gdcmException &e);
69
70 protected:
71    /// error message part 1
72    std::string From;
73    /// error message part 2
74    std::string Error;
75 };
76
77
78 //-----------------------------------------------------------------------------
79 /*
80  * File error exception thrown in the gdcm library
81  */
82 class GDCM_EXPORT gdcmFileError : public gdcmException
83 {
84 public:
85    /**
86     * \brief Builds an file-related exception with minimal information: name of
87     *               the thrower method and error message
88     * @param from name of the thrower
89     * @param error error description string
90     */
91    explicit gdcmFileError(const std::string &from,
92                           const std::string &error = "File error")
93       throw() : gdcmException(from, error) { }
94 };
95
96
97 //-----------------------------------------------------------------------------
98 /**
99  * \brief Unexpected file format exception
100  */
101 class GDCM_EXPORT gdcmFormatUnexpected : public gdcmException
102 {
103 public:
104    /// \brief Builds a file-related exception with minimal information:
105    /// name of the thrower method and error message
106    /// @param from name of the thrower
107    /// @param error error description string
108    explicit gdcmFormatUnexpected(const std::string &from,
109                             const std::string &error = "Unexpected file format")
110       throw() : gdcmException( from, error ) { }
111 };
112
113 //-----------------------------------------------------------------------------
114 /**
115  * \brief Invalid file format exception
116  */
117 class GDCM_EXPORT gdcmFormatError : public gdcmFormatUnexpected
118 {
119 public:
120    /// \brief Builds a file-related exception with minimal information:
121    /// name of the thrower method and error message
122    /// @param from name of the thrower
123    /// @param error error description string
124    explicit gdcmFormatError(const std::string &from,
125                             const std::string &error = "Invalid file format")
126       throw() : gdcmFormatUnexpected( from, error ) { }
127 };
128
129 //-----------------------------------------------------------------------------
130 /* prints exception stack on output stream
131  * @param os output stream
132  * @param e exception to print
133  * @returns output stream os
134  */
135 std::ostream& operator<<(std::ostream &os, const gdcmException &e);
136
137 //-----------------------------------------------------------------------------
138 #endif // GDCM_EXCEPTION_H