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