Changed license from GPL v2+ to FreeBSD (aka BSD 2-clause) license
[libdai.git] / include / dai / exceptions.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines the Exception class and macros for throwing exceptions and doing assertions
11
12
13 #ifndef __defined_libdai_exceptions_h
14 #define __defined_libdai_exceptions_h
15
16
17 #include <exception>
18 #include <stdexcept>
19 #include <string>
20 #include <iostream>
21
22
23 /// Used by DAI_THROW
24 #define DAI_QUOTE(x) #x
25
26 /// Used by DAI_THROW
27 #define DAI_TOSTRING(x) DAI_QUOTE(x)
28
29 /// Macro that simplifies throwing an exception with a useful default error message.
30 /** The error message consists of a description of the exception, the source
31 * code file and line number where the exception has been thrown.
32 * \param cod Corresponds to one of the enum values of dai::Exception::Code
33 *
34 * \par Example:
35 * \code
36 * DAI_THROW(NOT_IMPLEMENTED);
37 * \endcode
38 */
39 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
40
41 /// Macro that simplifies throwing an exception with a user-defined error message.
42 /** \param cod Corresponds to one of the enum values of dai::Exception::Code
43 * \param msg Detailed error message that will be written to std::cerr.
44 *
45 * \par Example:
46 * \code
47 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
48 * \endcode
49 */
50 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
51
52 /// Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is defined
53 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, std::string("Assertion \"" #condition "\" failed")))
54
55 // Assertion only if DAI_DEBUG is defined
56 #ifdef DAI_DEBUG
57 /// Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined
58 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
59 #else
60 #define DAI_DEBASSERT(x) do {} while(0)
61 #endif
62
63
64 namespace dai {
65
66
67 /// Error handling in libDAI is done by throwing an instance of the Exception class.
68 /** The Exception class inherits from std::runtime_error. It defines several types of exceptions
69 * and corresponding error messages. The recommended way to throw an instance of the Exception
70 * class is by using the #DAI_THROW or #DAI_THROWE macros.
71 */
72 class Exception : public std::runtime_error {
73 public:
74 /// Enumeration of exceptions used in libDAI
75 enum Code {NOT_IMPLEMENTED,
76 ASSERTION_FAILED,
77 IMPOSSIBLE_TYPECAST,
78 OBJECT_NOT_FOUND,
79 BELIEF_NOT_AVAILABLE,
80 UNKNOWN_ENUM_VALUE,
81 UNKNOWN_DAI_ALGORITHM,
82 UNKNOWN_PARAMETER_ESTIMATION_METHOD,
83 UNKNOWN_PROPERTY_TYPE,
84 UNKNOWN_PROPERTY,
85 MALFORMED_PROPERTY,
86 NOT_ALL_PROPERTIES_SPECIFIED,
87 INVALID_ALIAS,
88 CANNOT_READ_FILE,
89 CANNOT_WRITE_FILE,
90 INVALID_FACTORGRAPH_FILE,
91 INVALID_EVIDENCE_FILE,
92 INVALID_EMALG_FILE,
93 NOT_NORMALIZABLE,
94 MULTIPLE_UNDO,
95 FACTORGRAPH_NOT_CONNECTED,
96 INTERNAL_ERROR,
97 RUNTIME_ERROR,
98 OUT_OF_MEMORY,
99 NUM_ERRORS}; // NUM_ERRORS should be the last entry
100
101 /// Constructor
102 Exception( Code _code, const std::string& msg="", const std::string& detailedMsg="" ) : std::runtime_error(ErrorStrings[_code] + " [" + msg + "]"), errorcode(_code) {
103 if( !detailedMsg.empty() )
104 std::cerr << "EXCEPTION: " << detailedMsg << std::endl;
105 }
106
107 /// Returns error code of this exception
108 Code code() const { return errorcode; }
109
110 /// Returns error message corresponding to an error code
111 const std::string& message( const Code c ) const { return ErrorStrings[c]; }
112
113
114 private:
115 /// Contains the error code of this exception
116 Code errorcode;
117
118 /// Error messages corresponding to the exceptions enumerated above
119 static std::string ErrorStrings[NUM_ERRORS];
120 };
121
122
123 }
124
125
126 #endif