]> Creatis software - gdcm.git/blob - src/gdcmException.h
95d9a3c56115ee3b6fc85c2ccdc450b9d7d84fa4
[gdcm.git] / src / gdcmException.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmException.h,v $
5   Language:  C++
6   Date:      $Date: 2004/06/20 18:08:47 $
7   Version:   $Revision: 1.13 $
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.htm 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 public:
33    /*
34     * Builds an exception with minimal information: name of the thrower
35     * method and error message
36     *
37     * @param from name of the thrower
38     * @param error error description string
39     */
40    explicit gdcmException(const std::string &from, const std::string &error = "")
41     throw();
42
43    /**
44     * \brief virtual destructor makes this class dynamic
45     */
46    virtual ~gdcmException() throw() {
47    }
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(void) 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 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 gdcmFileError(const std::string &from,
91                           const std::string &error = "File error")
92       throw() : gdcmException(from, error) {
93    }
94 };
95
96
97 //-----------------------------------------------------------------------------
98 /*
99  * Invalid file format exception
100  */
101 class GDCM_EXPORT gdcmFormatError : public gdcmException {
102 public:
103    /*
104     * Builds an file-related exception with minimal information: name of
105     * the thrower method and error message
106     *
107     * @param from name of the thrower
108     * @param error error description string
109     */
110    explicit gdcmFormatError(const std::string &from,
111                             const std::string &error = "Invalid file format error")
112       throw() : gdcmException(from, error) {
113    }
114 };
115
116 //-----------------------------------------------------------------------------
117 /* prints exception stack on output stream
118  * @param os output stream
119  * @param e exception to print
120  * @returns output stream os
121  */
122 std::ostream& operator<<(std::ostream &os, const gdcmException &e);
123
124 //-----------------------------------------------------------------------------
125 #endif // GDCM_EXCEPTION_H