-/* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
- Radboud University Nijmegen, The Netherlands
-
+/* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
+ Radboud University Nijmegen, The Netherlands /
+ Max Planck Institute for Biological Cybernetics, Germany
+
This file is part of libDAI.
libDAI is free software; you can redistribute it and/or modify
*/
+/// \file
+/// \brief Defines Exception class and the DAI_THROW macro
+/// \todo Improve documentation
+
+
#ifndef __defined_libdai_exceptions_h
#define __defined_libdai_exceptions_h
#include <string>
+/// Used by DAI_THROW
#define DAI_QUOTE(x) #x
+
+/// Used by DAI_THROW
#define DAI_TOSTRING(x) DAI_QUOTE(x)
+/// Macro that simplifies throwing an exceptions with a useful error message
+/** \param cod Corresponds to one of the enum values in dai::Exception::codes
+ *
+ * Example:
+ * \code
+ * DAI_THROW(NOT_IMPLEMENTED);
+ * \endcode
+ */
#define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
namespace dai {
- class Exception : public std::runtime_error {
- public:
- Exception(size_t code, const std::string& msg = "") : std::runtime_error(ErrorStrings[code] + " [" + msg + "]") {}
-
- enum {NOT_IMPLEMENTED,
- UNKNOWN_DAI_ALGORITHM,
- UNKNOWN_PROPERTY_TYPE,
- MALFORMED_PROPERTY,
- UNKNOWN_ENUM_VALUE,
- CANNOT_READ_FILE,
- CANNOT_WRITE_FILE,
- INVALID_FACTORGRAPH_FILE,
- NOT_ALL_PROPERTIES_SPECIFIED,
- MULTIPLE_UNDO,
- FACTORGRAPH_NOT_CONNECTED,
- INTERNAL_ERROR,
- NUM_ERRORS}; // NUM_ERRORS should be the last entry
-
- private:
- static std::string ErrorStrings[NUM_ERRORS];
- };
+/// Represents an exception (based on std::runtime_error)
+class Exception : public std::runtime_error {
+ public:
+ /// Enumeration of exceptions used in libDAI
+ enum Code {NOT_IMPLEMENTED,
+ UNKNOWN_DAI_ALGORITHM,
+ UNKNOWN_PROPERTY_TYPE,
+ MALFORMED_PROPERTY,
+ UNKNOWN_ENUM_VALUE,
+ CANNOT_READ_FILE,
+ CANNOT_WRITE_FILE,
+ INVALID_FACTORGRAPH_FILE,
+ NOT_ALL_PROPERTIES_SPECIFIED,
+ MULTIPLE_UNDO,
+ FACTORGRAPH_NOT_CONNECTED,
+ IMPOSSIBLE_TYPECAST,
+ INTERNAL_ERROR,
+ NOT_NORMALIZABLE,
+ INVALID_EVIDENCE_FILE,
+ INVALID_EVIDENCE_LINE,
+ INVALID_EVIDENCE_OBSERVATION,
+ INVALID_SHARED_PARAMETERS_ORDER,
+ INVALID_SHARED_PARAMETERS_INPUT_LINE,
+ UNKNOWN_PARAMETER_ESTIMATION_METHOD,
+ NUM_ERRORS}; // NUM_ERRORS should be the last entry
+
+ /// Constructor
+ Exception( Code _code, const std::string& msg="" ) : std::runtime_error(ErrorStrings[_code] + " [" + msg + "]"), errorcode(_code) {}
+
+ /// Copy constructor
+ Exception( const Exception &e ) : std::runtime_error(e), errorcode(e.errorcode) {}
+
+ /// Returns error code of this exception
+ Code code() const { return errorcode; }
+
+
+ private:
+ /// Contains the error code of this exception
+ Code errorcode;
+
+ /// Error messages corresponding to the exceptions enumerated above
+ static std::string ErrorStrings[NUM_ERRORS];
+};
}