libDAI
exceptions.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_exceptions_h
14 #define __defined_libdai_exceptions_h
15 
16 
17 #include <exception>
18 #include <stdexcept>
19 #include <string>
20 #include <iostream>
21 
22 
24 #define DAI_QUOTE(x) #x
25 
27 #define DAI_TOSTRING(x) DAI_QUOTE(x)
28 
30 
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__
45 #endif
46 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, __FILE__, FUNCTION_NAME, DAI_TOSTRING(__LINE__), "")
47 
49 
57 #define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, __FILE__, FUNCTION_NAME, DAI_TOSTRING(__LINE__), msg)
58 
60 #define DAI_ASSERT(condition) ((condition) ? ((void)0) : DAI_THROWE(ASSERTION_FAILED, std::string("Assertion \"" #condition "\" failed")))
61 
62 // Assertion only if DAI_DEBUG is defined
63 #ifdef DAI_DEBUG
64 #define DAI_DEBASSERT(x) do {DAI_ASSERT(x);} while(0)
66 #else
67 #define DAI_DEBASSERT(x) do {} while(0)
68 #endif
69 
70 
71 namespace dai {
72 
73 
75 
79 class Exception : public std::runtime_error {
80  public:
82  enum Code {NOT_IMPLEMENTED,
83  ASSERTION_FAILED,
84  IMPOSSIBLE_TYPECAST,
85  OBJECT_NOT_FOUND,
86  BELIEF_NOT_AVAILABLE,
87  UNKNOWN_ENUM_VALUE,
88  UNKNOWN_DAI_ALGORITHM,
89  UNKNOWN_PARAMETER_ESTIMATION_METHOD,
90  UNKNOWN_PROPERTY_TYPE,
91  UNKNOWN_PROPERTY,
92  MALFORMED_PROPERTY,
93  NOT_ALL_PROPERTIES_SPECIFIED,
94  INVALID_ALIAS,
95  CANNOT_READ_FILE,
96  CANNOT_WRITE_FILE,
97  INVALID_FACTORGRAPH_FILE,
98  INVALID_EVIDENCE_FILE,
99  INVALID_EMALG_FILE,
100  NOT_NORMALIZABLE,
101  MULTIPLE_UNDO,
102  FACTORGRAPH_NOT_CONNECTED,
103  INTERNAL_ERROR,
104  RUNTIME_ERROR,
105  OUT_OF_MEMORY,
106  NUM_ERRORS}; // NUM_ERRORS should be the last entry
107 
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) {}
112 
114  ~Exception() throw () {}
115 
117  Code getCode() const { return _errorcode; }
118 
120 
122  Code code() const { return getCode(); }
123 
125  const std::string& getMsg() const { return ErrorStrings[_errorcode]; }
126 
128  const std::string& getDetailedMsg() const { return _detailedMsg; }
129 
131  const std::string& getFilename() const { return _filename; }
132 
134  const std::string& getFunction() const { return _function; }
135 
137  const std::string& getLine() const { return _line; }
138 
140  const std::string& message( const Code c ) const { return ErrorStrings[c]; }
141 
142  private:
145 
147  std::string _detailedMsg;
148 
150  std::string _filename;
151 
153  std::string _function;
154 
156  std::string _line;
157 
159  static std::string ErrorStrings[NUM_ERRORS];
160 };
161 
162 
163 }
164 
165 
166 #endif
Code getCode() const
Returns error code of this exception.
Definition: exceptions.h:117
Code code() const
Returns error code of this exception.
Definition: exceptions.h:122
static std::string ErrorStrings[NUM_ERRORS]
Error messages corresponding to the exceptions enumerated above.
Definition: exceptions.h:159
STL namespace.
~Exception()
Destructor.
Definition: exceptions.h:114
Error handling in libDAI is done by throwing an instance of the Exception class.
Definition: exceptions.h:79
std::string _detailedMsg
Contains the detailed message of this exception, if any.
Definition: exceptions.h:147
Exception(Code code, const char *filename, const char *function, const char *line, const std::string &detailedMsg)
Constructor.
Definition: exceptions.h:109
const std::string & message(const Code c) const
Returns error message corresponding to an error code.
Definition: exceptions.h:140
const std::string & getFilename() const
Returns filename where this exception was thrown.
Definition: exceptions.h:131
std::string _line
Contains the line number where this exception was thrown.
Definition: exceptions.h:156
Code _errorcode
Contains the error code of this exception.
Definition: exceptions.h:144
const std::string & getDetailedMsg() const
Returns detailed error message of this exception.
Definition: exceptions.h:128
std::string _filename
Contains the filename where this exception was thrown.
Definition: exceptions.h:150
const std::string & getFunction() const
Returns function name in which this exception was thrown.
Definition: exceptions.h:134
const std::string & getLine() const
Returns line number where this exception was thrown.
Definition: exceptions.h:137
Namespace for libDAI.
Definition: alldai.cpp:16
std::string _function
Contains the function name in which this exception was thrown.
Definition: exceptions.h:153
Code
Enumeration of exceptions used in libDAI.
Definition: exceptions.h:82
const std::string & getMsg() const
Returns short error message of this exception.
Definition: exceptions.h:125