1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 /// \brief Defines the Exception class and macros for throwing exceptions and doing assertions
16 #ifndef __defined_libdai_exceptions_h
17 #define __defined_libdai_exceptions_h
27 #define DAI_QUOTE(x) #x
30 #define DAI_TOSTRING(x) DAI_QUOTE(x)
32 /// Macro that simplifies throwing an exception with a useful default error message.
33 /** The error message consists of a description of the exception, the source
34 * code file and line number where the exception has been thrown.
35 * \param cod Corresponds to one of the enum values of dai::Exception::Code
39 * DAI_THROW(NOT_IMPLEMENTED);
42 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
44 /// Macro that simplifies throwing an exception with a user-defined error message.
45 /** \param cod Corresponds to one of the enum values of dai::Exception::Code
46 * \param msg Detailed error message that will be written to std::cerr.
50 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
53 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
55 /// Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is defined
56 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, std::string("Assertion \"" #condition "\" failed")))
58 // Assertion only if DAI_DEBUG is defined
60 /// Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined
61 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
63 #define DAI_DEBASSERT(x) do {} while(0)
70 /// Error handling in libDAI is done by throwing an instance of the Exception class.
71 /** The Exception class inherits from std::runtime_error. It defines several types of exceptions
72 * and corresponding error messages. The recommended way to throw an instance of the Exception
73 * class is by using the #DAI_THROW or #DAI_THROWE macros.
75 class Exception
: public std::runtime_error
{
77 /// Enumeration of exceptions used in libDAI
78 enum Code
{NOT_IMPLEMENTED
,
83 UNKNOWN_DAI_ALGORITHM
,
84 UNKNOWN_PARAMETER_ESTIMATION_METHOD
,
85 UNKNOWN_PROPERTY_TYPE
,
87 NOT_ALL_PROPERTIES_SPECIFIED
,
90 INVALID_FACTORGRAPH_FILE
,
91 INVALID_EVIDENCE_FILE
,
95 FACTORGRAPH_NOT_CONNECTED
,
98 NUM_ERRORS
}; // NUM_ERRORS should be the last entry
101 Exception( Code _code
, const std::string
& msg
="", const std::string
& detailedMsg
="" ) : std::runtime_error(ErrorStrings
[_code
] + " [" + msg
+ "]"), errorcode(_code
) {
102 if( !detailedMsg
.empty() )
103 std::cerr
<< "ERROR: " << detailedMsg
<< std::endl
;
106 /// Returns error code of this exception
107 Code
code() const { return errorcode
; }
109 /// Returns error message corresponding to an error code
110 const std::string
&message( const Code c
) const { return ErrorStrings
[c
]; }
114 /// Contains the error code of this exception
117 /// Error messages corresponding to the exceptions enumerated above
118 static std::string ErrorStrings
[NUM_ERRORS
];