1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 This file is part of libDAI.
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 /// \brief Defines Exception class and the DAI_THROW macro
25 /// \todo Improve documentation
28 #ifndef __defined_libdai_exceptions_h
29 #define __defined_libdai_exceptions_h
38 #define DAI_QUOTE(x) #x
41 #define DAI_TOSTRING(x) DAI_QUOTE(x)
43 /// Macro that simplifies throwing an exceptions with a useful error message
44 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
48 * DAI_THROW(NOT_IMPLEMENTED);
51 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
57 /// Represents an exception (based on std::runtime_error)
58 class Exception
: public std::runtime_error
{
60 /// Enumeration of exceptions used in libDAI
61 enum Code
{NOT_IMPLEMENTED
,
62 UNKNOWN_DAI_ALGORITHM
,
63 UNKNOWN_PROPERTY_TYPE
,
68 INVALID_FACTORGRAPH_FILE
,
69 NOT_ALL_PROPERTIES_SPECIFIED
,
71 FACTORGRAPH_NOT_CONNECTED
,
75 NUM_ERRORS
}; // NUM_ERRORS should be the last entry
78 Exception( Code _code
, const std::string
& msg
="" ) : std::runtime_error(ErrorStrings
[_code
] + " [" + msg
+ "]"), errorcode(_code
) {}
81 Exception( const Exception
&e
) : std::runtime_error(e
), errorcode(e
.errorcode
) {}
83 /// Returns error code of this exception
84 Code
code() const { return errorcode
; }
88 /// Contains the error code of this exception
91 /// Error messages corresponding to the exceptions enumerated above
92 static std::string ErrorStrings
[NUM_ERRORS
];