Cleaned up error handling by introducing the DAI_THROWE macro.
[libdai.git] / include / dai / exceptions.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines Exception class and the DAI_THROW macro
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_exceptions_h
29 #define __defined_libdai_exceptions_h
30
31
32 #include <exception>
33 #include <stdexcept>
34 #include <string>
35 #include <iostream>
36
37
38 /// Used by DAI_THROW
39 #define DAI_QUOTE(x) #x
40
41 /// Used by DAI_THROW
42 #define DAI_TOSTRING(x) DAI_QUOTE(x)
43
44 /// Macro that simplifies throwing an exception with a useful error message.
45 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
46 *
47 * Example:
48 * \code
49 * DAI_THROW(NOT_IMPLEMENTED);
50 * \endcode
51 */
52 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
53
54 /// Macro that simplifies throwing an exception with a useful error message. It also allows for writing a detailed error message to stderr.
55 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
56 * \param msg Detailed error message that will be written to std::cerr.
57 *
58 * Example:
59 * \code
60 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
61 * \endcode
62 */
63 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
64
65
66 namespace dai {
67
68
69 /// Represents an exception (based on std::runtime_error)
70 class Exception : public std::runtime_error {
71 public:
72 /// Enumeration of exceptions used in libDAI
73 enum Code {NOT_IMPLEMENTED,
74 UNKNOWN_DAI_ALGORITHM,
75 UNKNOWN_PROPERTY_TYPE,
76 MALFORMED_PROPERTY,
77 UNKNOWN_ENUM_VALUE,
78 CANNOT_READ_FILE,
79 CANNOT_WRITE_FILE,
80 INVALID_FACTORGRAPH_FILE,
81 NOT_ALL_PROPERTIES_SPECIFIED,
82 MULTIPLE_UNDO,
83 FACTORGRAPH_NOT_CONNECTED,
84 IMPOSSIBLE_TYPECAST,
85 INTERNAL_ERROR,
86 RUNTIME_ERROR,
87 NOT_NORMALIZABLE,
88 INVALID_EVIDENCE_FILE,
89 INVALID_EMALG_FILE,
90 UNKNOWN_PARAMETER_ESTIMATION_METHOD,
91 NUM_ERRORS}; // NUM_ERRORS should be the last entry
92
93 /// Constructor
94 Exception( Code _code, const std::string& msg="", const std::string& detailedMsg="" ) : std::runtime_error(ErrorStrings[_code] + " [" + msg + "]"), errorcode(_code) {
95 if( !detailedMsg.empty() )
96 std::cerr << "EXCEPTION: " << detailedMsg << std::endl;
97 }
98
99 /// Copy constructor
100 Exception( const Exception &e ) : std::runtime_error(e), errorcode(e.errorcode) {}
101
102 /// Returns error code of this exception
103 Code code() const { return errorcode; }
104
105
106 private:
107 /// Contains the error code of this exception
108 Code errorcode;
109
110 /// Error messages corresponding to the exceptions enumerated above
111 static std::string ErrorStrings[NUM_ERRORS];
112 };
113
114
115 }
116
117
118 #endif