Fixed tabs and trailing whitespaces
[libdai.git] / include / dai / exceptions.h
index 0ecb329..87f8876 100644 (file)
@@ -32,6 +32,7 @@
 #include <exception>
 #include <stdexcept>
 #include <string>
+#include <iostream>
 
 
 /// Used by DAI_THROW
 /// Used by DAI_THROW
 #define DAI_TOSTRING(x) DAI_QUOTE(x)
 
-/// Macro that simplifies throwing an exceptions with a useful error message
+/// Macro that simplifies throwing an exception with a useful error message.
 /** \param cod Corresponds to one of the enum values in dai::Exception::codes
  *
- * Example:
+ *  Example:
  *  \code
  *  DAI_THROW(NOT_IMPLEMENTED);
  *  \endcode
  */
 #define DAI_THROW(cod) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)))
 
+/// Macro that simplifies throwing an exception with a useful error message. It also allows for writing a detailed error message to stderr.
+/** \param cod Corresponds to one of the enum values in dai::Exception::codes
+ *  \param msg Detailed error message that will be written to std::cerr.
+ *
+ *  Example:
+ *  \code
+ *  DAI_THROWE(NOT_IMPLEMENTED,"Detailed error message");
+ *  \endcode
+ */
+#define DAI_THROWE(cod,msg) throw dai::Exception(dai::Exception::cod, std::string(__FILE__ ", line " DAI_TOSTRING(__LINE__)), msg)
+
 
 namespace dai {
 
@@ -57,26 +69,45 @@ namespace dai {
 /// 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,
+                   RUNTIME_ERROR,
+                   NOT_NORMALIZABLE,
+                   INVALID_EVIDENCE_FILE,
+                   INVALID_EMALG_FILE,
+                   UNKNOWN_PARAMETER_ESTIMATION_METHOD,
+                   OBJECT_NOT_FOUND,
+                   NUM_ERRORS};  // NUM_ERRORS should be the last entry
+
         /// Constructor
-            Exception(size_t code, const std::string& msg = "") : std::runtime_error(ErrorStrings[code] + " [" +  msg + "]") {}
+        Exception( Code _code, const std::string& msg="", const std::string& detailedMsg="" ) : std::runtime_error(ErrorStrings[_code] + " [" +  msg + "]"), errorcode(_code) {
+            if( !detailedMsg.empty() )
+                std::cerr << "ERROR: " << detailedMsg << std::endl;
+        }
+
+        /// Copy constructor
+        Exception( const Exception &e ) : std::runtime_error(e), errorcode(e.errorcode) {}
+
+        /// Returns error code of this exception
+        Code code() const { return errorcode; }
 
-        /// Enumeration of exceptions used in libDAI
-        enum codes {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,
-                    NUM_ERRORS};  // NUM_ERRORS should be the last entry
 
     private:
+        /// Contains the error code of this exception
+        Code errorcode;
+
         /// Error messages corresponding to the exceptions enumerated above
         static std::string ErrorStrings[NUM_ERRORS];
 };