1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
10 /// \brief Defines the Exception class and macros for throwing exceptions and doing assertions
13 #ifndef __defined_libdai_exceptions_h
14 #define __defined_libdai_exceptions_h
24 #define DAI_QUOTE(x) #x
27 #define DAI_TOSTRING(x) DAI_QUOTE(x)
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
36 * DAI_THROW(NOT_IMPLEMENTED);
39 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
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.
47 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
50 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
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")))
55 // Assertion only if DAI_DEBUG is defined
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)
60 #define DAI_DEBASSERT(x) do {} while(0)
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.
72 class Exception
: public std::runtime_error
{
74 /// Enumeration of exceptions used in libDAI
75 enum Code
{NOT_IMPLEMENTED
,
81 UNKNOWN_DAI_ALGORITHM
,
82 UNKNOWN_PARAMETER_ESTIMATION_METHOD
,
83 UNKNOWN_PROPERTY_TYPE
,
86 NOT_ALL_PROPERTIES_SPECIFIED
,
90 INVALID_FACTORGRAPH_FILE
,
91 INVALID_EVIDENCE_FILE
,
95 FACTORGRAPH_NOT_CONNECTED
,
99 NUM_ERRORS
}; // NUM_ERRORS should be the last entry
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
;
107 /// Returns error code of this exception
108 Code
code() const { return errorcode
; }
110 /// Returns error message corresponding to an error code
111 const std::string
& message( const Code c
) const { return ErrorStrings
[c
]; }
115 /// Contains the error code of this exception
118 /// Error messages corresponding to the exceptions enumerated above
119 static std::string ErrorStrings
[NUM_ERRORS
];