5339f9dcea0025d9e8c90f01a5e126951ef9db5e
[libdai.git] / include / dai / exceptions.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines Exception class and the DAI_THROW macro
14 /// \todo Improve documentation
15
16
17 #ifndef __defined_libdai_exceptions_h
18 #define __defined_libdai_exceptions_h
19
20
21 #include <exception>
22 #include <stdexcept>
23 #include <string>
24 #include <iostream>
25
26
27 /// Used by DAI_THROW
28 #define DAI_QUOTE(x) #x
29
30 /// Used by DAI_THROW
31 #define DAI_TOSTRING(x) DAI_QUOTE(x)
32
33 /// Macro that simplifies throwing an exception with a useful error message.
34 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
35 *
36 * Example:
37 * \code
38 * DAI_THROW(NOT_IMPLEMENTED);
39 * \endcode
40 */
41 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
42
43 /// Macro that simplifies throwing an exception with a useful error message. It also allows for writing a detailed error message to stderr.
44 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
45 * \param msg Detailed error message that will be written to std::cerr.
46 *
47 * Example:
48 * \code
49 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
50 * \endcode
51 */
52 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
53
54 /// Assertion mechanism, similar to the standard assert() macro, but is always active, even if NDEBUG is defined
55 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, #condition))
56
57 // Assertion only if DAI_DEBUG is defined
58 #ifdef DAI_DEBUG
59 /// Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined
60 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
61 #else
62 #define DAI_DEBASSERT(X) do {} while(0)
63 #endif
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 {ASSERTION_FAILED,
74 NOT_IMPLEMENTED,
75 UNKNOWN_DAI_ALGORITHM,
76 UNKNOWN_PROPERTY_TYPE,
77 MALFORMED_PROPERTY,
78 UNKNOWN_ENUM_VALUE,
79 CANNOT_READ_FILE,
80 CANNOT_WRITE_FILE,
81 INVALID_FACTORGRAPH_FILE,
82 NOT_ALL_PROPERTIES_SPECIFIED,
83 MULTIPLE_UNDO,
84 FACTORGRAPH_NOT_CONNECTED,
85 IMPOSSIBLE_TYPECAST,
86 INTERNAL_ERROR,
87 RUNTIME_ERROR,
88 NOT_NORMALIZABLE,
89 INVALID_EVIDENCE_FILE,
90 INVALID_EMALG_FILE,
91 UNKNOWN_PARAMETER_ESTIMATION_METHOD,
92 OBJECT_NOT_FOUND,
93 NUM_ERRORS}; // NUM_ERRORS should be the last entry
94
95 /// Constructor
96 Exception( Code _code, const std::string& msg="", const std::string& detailedMsg="" ) : std::runtime_error(ErrorStrings[_code] + " [" + msg + "]"), errorcode(_code) {
97 if( !detailedMsg.empty() )
98 std::cerr << "ERROR: " << detailedMsg << std::endl;
99 }
100
101 /// Copy constructor
102 Exception( const Exception &e ) : std::runtime_error(e), errorcode(e.errorcode) {}
103
104 /// Returns error code of this exception
105 Code code() const { return errorcode; }
106
107
108 private:
109 /// Contains the error code of this exception
110 Code errorcode;
111
112 /// Error messages corresponding to the exceptions enumerated above
113 static std::string ErrorStrings[NUM_ERRORS];
114 };
115
116
117 }
118
119
120 #endif