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 Exception class and the DAI_THROW macro
14 /// \todo Improve documentation
17 #ifndef __defined_libdai_exceptions_h
18 #define __defined_libdai_exceptions_h
28 #define DAI_QUOTE(x) #x
31 #define DAI_TOSTRING(x) DAI_QUOTE(x)
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
38 * DAI_THROW(NOT_IMPLEMENTED);
41 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
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.
49 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
52 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
58 /// Represents an exception (based on std::runtime_error)
59 class Exception
: public std::runtime_error
{
61 /// Enumeration of exceptions used in libDAI
62 enum Code
{NOT_IMPLEMENTED
,
63 UNKNOWN_DAI_ALGORITHM
,
64 UNKNOWN_PROPERTY_TYPE
,
69 INVALID_FACTORGRAPH_FILE
,
70 NOT_ALL_PROPERTIES_SPECIFIED
,
72 FACTORGRAPH_NOT_CONNECTED
,
77 INVALID_EVIDENCE_FILE
,
79 UNKNOWN_PARAMETER_ESTIMATION_METHOD
,
81 NUM_ERRORS
}; // NUM_ERRORS should be the last entry
84 Exception( Code _code
, const std::string
& msg
="", const std::string
& detailedMsg
="" ) : std::runtime_error(ErrorStrings
[_code
] + " [" + msg
+ "]"), errorcode(_code
) {
85 if( !detailedMsg
.empty() )
86 std::cerr
<< "ERROR: " << detailedMsg
<< std::endl
;
90 Exception( const Exception
&e
) : std::runtime_error(e
), errorcode(e
.errorcode
) {}
92 /// Returns error code of this exception
93 Code
code() const { return errorcode
; }
97 /// Contains the error code of this exception
100 /// Error messages corresponding to the exceptions enumerated above
101 static std::string ErrorStrings
[NUM_ERRORS
];