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