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 #if defined __GNUG__ // GNU C++
40 #define FUNCTION_NAME __PRETTY_FUNCTION__
41 #elif defined _MSC_VER // Visual Studio
42 #define FUNCTION_NAME __FUNCTION__
43 #else // other compilers
44 #define FUNCTION_NAME __func__
46 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, __FILE__, FUNCTION_NAME, DAI_TOSTRING(__LINE__), "")
48 /// Macro that simplifies throwing an exception with a user-defined error message.
49 /** \param cod Corresponds to one of the enum values of dai::Exception::Code
50 * \param msg Detailed error message that will be written to std::cerr.
54 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
57 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, __FILE__, FUNCTION_NAME, DAI_TOSTRING(__LINE__), msg)
59 /// Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is defined
60 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, std::string("Assertion \"" #condition "\" failed")))
62 // Assertion only if DAI_DEBUG is defined
64 /// Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined
65 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
67 #define DAI_DEBASSERT(x) do {} while(0)
74 /// Error handling in libDAI is done by throwing an instance of the Exception class.
75 /** The Exception class inherits from std::runtime_error. It defines several types of exceptions
76 * and corresponding error messages. The recommended way to throw an instance of the Exception
77 * class is by using the #DAI_THROW or #DAI_THROWE macros.
79 class Exception
: public std::runtime_error
{
81 /// Enumeration of exceptions used in libDAI
82 enum Code
{NOT_IMPLEMENTED
,
88 UNKNOWN_DAI_ALGORITHM
,
89 UNKNOWN_PARAMETER_ESTIMATION_METHOD
,
90 UNKNOWN_PROPERTY_TYPE
,
93 NOT_ALL_PROPERTIES_SPECIFIED
,
97 INVALID_FACTORGRAPH_FILE
,
98 INVALID_EVIDENCE_FILE
,
102 FACTORGRAPH_NOT_CONNECTED
,
106 NUM_ERRORS
}; // NUM_ERRORS should be the last entry
109 Exception( Code code
, const char *filename
, const char *function
, const char *line
, const std::string
& detailedMsg
) :
110 std::runtime_error(ErrorStrings
[code
] + (detailedMsg
.empty() ? "" : (": " + detailedMsg
)) + " [File " + filename
+ ", line " + line
+ ", function: " + function
+ "]"),
111 _errorcode(code
), _detailedMsg(detailedMsg
), _filename(filename
), _function(function
), _line(line
) {}
114 ~Exception() throw () {}
116 /// Returns error code of this exception
117 Code
getCode() const { return _errorcode
; }
119 /// Returns error code of this exception
120 /** \deprecated Please use dai::Exceptions::getCode() instead
122 Code
code() const { return getCode(); }
124 /// Returns short error message of this exception
125 const std::string
& getMsg() const { return ErrorStrings
[_errorcode
]; }
127 /// Returns detailed error message of this exception
128 const std::string
& getDetailedMsg() const { return _detailedMsg
; }
130 /// Returns filename where this exception was thrown
131 const std::string
& getFilename() const { return _filename
; }
133 /// Returns function name in which this exception was thrown
134 const std::string
& getFunction() const { return _function
; }
136 /// Returns line number where this exception was thrown
137 const std::string
& getLine() const { return _line
; }
139 /// Returns error message corresponding to an error code
140 const std::string
& message( const Code c
) const { return ErrorStrings
[c
]; }
143 /// Contains the error code of this exception
146 /// Contains the detailed message of this exception, if any
147 std::string _detailedMsg
;
149 /// Contains the filename where this exception was thrown
150 std::string _filename
;
152 /// Contains the function name in which this exception was thrown
153 std::string _function
;
155 /// Contains the line number where this exception was thrown
158 /// Error messages corresponding to the exceptions enumerated above
159 static std::string ErrorStrings
[NUM_ERRORS
];