0f5c420785960a629acce7192280db0da863a739
[libdai.git] / include / dai / exceptions.h
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
4
5 This file is part of libDAI.
6
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.
11
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.
16
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
20 */
21
22
23 /// \file
24 /// \brief Defines Exception class and the DAI_THROW macro
25
26
27 #ifndef __defined_libdai_exceptions_h
28 #define __defined_libdai_exceptions_h
29
30
31 #include <exception>
32 #include <stdexcept>
33 #include <string>
34
35
36 /// Used by DAI_THROW
37 #define DAI_QUOTE(x) #x
38
39 /// Used by DAI_THROW
40 #define DAI_TOSTRING(x) DAI_QUOTE(x)
41
42 /// Macro that simplifies throwing an exceptions with a useful error message
43 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
44 *
45 * Example:
46 * \code
47 * DAI_THROW(NOT_IMPLEMENTED);
48 * \endcode
49 */
50 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
51
52
53 namespace dai {
54
55
56 /// Represents an exception (based on std::runtime_error)
57 class Exception : public std::runtime_error {
58 public:
59 /// Constructor
60 Exception(size_t code, const std::string& msg = "") : std::runtime_error(ErrorStrings[code] + " [" + msg + "]") {}
61
62 /// Enumeration of exceptions used in libDAI
63 enum codes {NOT_IMPLEMENTED,
64 UNKNOWN_DAI_ALGORITHM,
65 UNKNOWN_PROPERTY_TYPE,
66 MALFORMED_PROPERTY,
67 UNKNOWN_ENUM_VALUE,
68 CANNOT_READ_FILE,
69 CANNOT_WRITE_FILE,
70 INVALID_FACTORGRAPH_FILE,
71 NOT_ALL_PROPERTIES_SPECIFIED,
72 MULTIPLE_UNDO,
73 FACTORGRAPH_NOT_CONNECTED,
74 IMPOSSIBLE_TYPECAST,
75 INTERNAL_ERROR,
76 NUM_ERRORS}; // NUM_ERRORS should be the last entry
77
78 private:
79 /// Error messages corresponding to the exceptions enumerated above
80 static std::string ErrorStrings[NUM_ERRORS];
81 };
82
83
84 }
85
86
87 #endif