3768a7a1f35d27fc329804ba70721f0a9153974c
[libdai.git] / include / dai / exceptions.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines Exception class and the DAI_THROW macro
14 /// \todo Improve documentation
15
16
17 #ifndef __defined_libdai_exceptions_h
18 #define __defined_libdai_exceptions_h
19
20
21 #include <exception>
22 #include <stdexcept>
23 #include <string>
24 #include <iostream>
25
26
27 /// Used by DAI_THROW
28 #define DAI_QUOTE(x) #x
29
30 /// Used by DAI_THROW
31 #define DAI_TOSTRING(x) DAI_QUOTE(x)
32
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
35 *
36 * Example:
37 * \code
38 * DAI_THROW(NOT_IMPLEMENTED);
39 * \endcode
40 */
41 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
42
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.
46 *
47 * Example:
48 * \code
49 * DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
50 * \endcode
51 */
52 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
53
54
55 namespace dai {
56
57
58 /// Represents an exception (based on std::runtime_error)
59 class Exception : public std::runtime_error {
60 public:
61 /// Enumeration of exceptions used in libDAI
62 enum Code {NOT_IMPLEMENTED,
63 UNKNOWN_DAI_ALGORITHM,
64 UNKNOWN_PROPERTY_TYPE,
65 MALFORMED_PROPERTY,
66 UNKNOWN_ENUM_VALUE,
67 CANNOT_READ_FILE,
68 CANNOT_WRITE_FILE,
69 INVALID_FACTORGRAPH_FILE,
70 NOT_ALL_PROPERTIES_SPECIFIED,
71 MULTIPLE_UNDO,
72 FACTORGRAPH_NOT_CONNECTED,
73 IMPOSSIBLE_TYPECAST,
74 INTERNAL_ERROR,
75 RUNTIME_ERROR,
76 NOT_NORMALIZABLE,
77 INVALID_EVIDENCE_FILE,
78 INVALID_EMALG_FILE,
79 UNKNOWN_PARAMETER_ESTIMATION_METHOD,
80 OBJECT_NOT_FOUND,
81 NUM_ERRORS}; // NUM_ERRORS should be the last entry
82
83 /// Constructor
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;
87 }
88
89 /// Copy constructor
90 Exception( const Exception &e ) : std::runtime_error(e), errorcode(e.errorcode) {}
91
92 /// Returns error code of this exception
93 Code code() const { return errorcode; }
94
95
96 private:
97 /// Contains the error code of this exception
98 Code errorcode;
99
100 /// Error messages corresponding to the exceptions enumerated above
101 static std::string ErrorStrings[NUM_ERRORS];
102 };
103
104
105 }
106
107
108 #endif