[pyAgrum] Better exception handling

parent e91a95d8
...@@ -36,22 +36,12 @@ ...@@ -36,22 +36,12 @@
#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg)) #define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
#ifdef SWIG #ifdef SWIG
#ifdef NDEBUG
#define GUM_ERROR(type, msg) \
{ \
std::ostringstream __error__str; \
__error__str << __FILE__ << ":" << __LINE__ << ": " << msg; \
throw(type(__error__str.str())); \
}
#else
#define GUM_ERROR(type, msg) \ #define GUM_ERROR(type, msg) \
{ \ { \
std::ostringstream __error__str; \ std::ostringstream __error__str; \
__error__str << msg; \ __error__str << msg; \
throw(type(__error__str.str())); \ throw(type(__error__str.str())); \
} }
#endif // NDEBUG
#define GUM_SHOWERROR(e) \ #define GUM_SHOWERROR(e) \
{ \ { \
std::cout << std::endl \ std::cout << std::endl \
...@@ -59,7 +49,6 @@ ...@@ -59,7 +49,6 @@
} }
#else #else
#ifdef NDEBUG #ifdef NDEBUG
#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
#define GUM_ERROR(type, msg) \ #define GUM_ERROR(type, msg) \
{ \ { \
std::ostringstream __error__str; \ std::ostringstream __error__str; \
...@@ -74,7 +63,6 @@ ...@@ -74,7 +63,6 @@
<< (e).errorContent() << std::endl; \ << (e).errorContent() << std::endl; \
} }
#else #else
#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
#define GUM_ERROR(type, msg) \ #define GUM_ERROR(type, msg) \
{ \ { \
std::ostringstream __error__str; \ std::ostringstream __error__str; \
...@@ -98,6 +86,8 @@ ...@@ -98,6 +86,8 @@
public: \ public: \
TYPE(std::string aMsg, std::string aType = MSG) \ TYPE(std::string aMsg, std::string aType = MSG) \
: SUPERCLASS(aMsg, aType){}; \ : SUPERCLASS(aMsg, aType){}; \
TYPE(const TYPE& src) \
: SUPERCLASS(src){}; \
}; };
#define GUM_SYNTAX_ERROR(msg, line, column) \ #define GUM_SYNTAX_ERROR(msg, line, column) \
...@@ -131,9 +121,12 @@ namespace gum { ...@@ -131,9 +121,12 @@ namespace gum {
~Exception() {} ~Exception() {}
/// @} /// @}
#ifdef SWIG
const std::string toString() const { return _msg; } const std::string what() const { return "[pyAgrum] "+_type + ": " + _msg; }
#else
const std::string what() const { return "[pyAgrum] "+ _type + " : " + _msg; }
#endif
/** /**
* @brief Returns the message content. * @brief Returns the message content.
......
/* EXCEPTION HANDLING */ /* EXCEPTION HANDLING */
%{ %{
#define PYGUM_CATCH(GUMEXCEPTION) \
catch (gum::GUMEXCEPTION & e) { \
PyErr_SetString (SWIG_Python_ExceptionType(SWIGTYPE_p_gum__##GUMEXCEPTION),e.what().c_str()); \
}
static void SetPythonizeAgrumException() { static void SetPythonizeAgrumException() {
try { try {
throw; throw;
} catch ( std::bad_cast& e ) { }
catch ( std::bad_cast& e ) {
PyErr_SetString ( PyExc_RuntimeError, "C++ Bad Cast" ); PyErr_SetString ( PyExc_RuntimeError, "C++ Bad Cast" );
} catch ( gum::NotFound& e ) { }
PyErr_SetString ( PyExc_IndexError, e.errorContent().c_str() ); PYGUM_CATCH(NotFound)
} catch ( gum::DefaultInLabel& e ) { PYGUM_CATCH(DefaultInLabel)
gum::DefaultInLabel* ecopy = new gum::DefaultInLabel ( e ); PYGUM_CATCH(IdError)
PyObject* err = SWIG_NewPointerObj ( ecopy, PYGUM_CATCH(IOError)
SWIGTYPE_p_gum__DefaultInLabel, PYGUM_CATCH(OperationNotAllowed)
1 ); PYGUM_CATCH(InvalidDirectedCycle)
PyErr_SetObject ( PYGUM_CATCH(InvalidNode)
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__DefaultInLabel ), err ); PYGUM_CATCH(InvalidArc)
} catch ( gum::IdError& e ) { PYGUM_CATCH(InvalidEdge)
gum::IdError* ecopy = new gum::IdError ( e ); PYGUM_CATCH(DuplicateLabel)
PyObject* err = SWIG_NewPointerObj ( ecopy, PYGUM_CATCH(DuplicateElement)
SWIGTYPE_p_gum__IdError, PYGUM_CATCH(OutOfLowerBound)
1 ); PYGUM_CATCH(OutOfUpperBound)
PyErr_SetObject ( PYGUM_CATCH(OutOfBounds)
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__IdError ), err ); PYGUM_CATCH(UnknownLabelInDatabase)
} catch ( gum::IOError& e ) { PYGUM_CATCH(FatalError)
gum::IOError* ecopy = new gum::IOError ( e ); PYGUM_CATCH(UndefinedElement)
PyObject* err = SWIG_NewPointerObj ( ecopy, PYGUM_CATCH(InvalidArgument)
SWIGTYPE_p_gum__IOError, catch ( gum::Exception& e ) {
1 ); PyErr_SetString ( PyExc_Exception, e.what().c_str() );
PyErr_SetObject ( }
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__IOError ), catch ( std::exception& e ) {
err );
} catch ( gum::OperationNotAllowed& e ) {
gum::OperationNotAllowed* ecopy = new gum::OperationNotAllowed ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__OperationNotAllowed,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__OperationNotAllowed ),
err );
} catch ( gum::InvalidDirectedCycle& e ) {
gum::InvalidDirectedCycle* ecopy = new gum::InvalidDirectedCycle ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__InvalidDirectedCycle,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__InvalidDirectedCycle ), err );
} catch ( gum::InvalidNode& e ) {
gum::InvalidNode* ecopy = new gum::InvalidNode ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__InvalidNode,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__InvalidNode ), err );
} catch ( gum::InvalidArc& e ) {
gum::InvalidArc* ecopy = new gum::InvalidArc ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__InvalidArc,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__InvalidArc ), err );
} catch ( gum::InvalidEdge& e ) {
gum::InvalidEdge* ecopy = new gum::InvalidEdge ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__InvalidEdge,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__InvalidEdge ), err );
} catch ( gum::DuplicateLabel& e ) {
gum::DuplicateLabel* ecopy = new gum::DuplicateLabel ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__DuplicateLabel,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__DuplicateLabel ), err );
} catch ( gum::DuplicateElement& e ) {
gum::DuplicateElement* ecopy = new gum::DuplicateElement ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__DuplicateElement,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__DuplicateElement ), err );
} catch ( gum::OutOfLowerBound& e ) {
gum::OutOfLowerBound* ecopy = new gum::OutOfLowerBound ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__OutOfLowerBound,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__OutOfLowerBound ), err );
} catch ( gum::OutOfUpperBound& e ) {
gum::OutOfUpperBound* ecopy = new gum::OutOfUpperBound ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__OutOfUpperBound,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__OutOfUpperBound ), err );
} catch ( gum::OutOfBounds& e ) {
gum::OutOfBounds* ecopy = new gum::OutOfBounds ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__OutOfBounds,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__OutOfBounds ), err );
} catch ( gum::UnknownLabelInDatabase& e ) {
gum::UnknownLabelInDatabase* ecopy = new gum::UnknownLabelInDatabase ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__UnknownLabelInDatabase,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__UnknownLabelInDatabase ), err );
} catch ( gum::FatalError& e ) {
gum::FatalError* ecopy = new gum::FatalError ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__FatalError,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__FatalError ), err );
} catch ( gum::UndefinedElement& e ) {
gum::UndefinedElement* ecopy = new gum::UndefinedElement ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__UndefinedElement,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__UndefinedElement ), err );
} catch ( gum::InvalidArgument& e ) {
gum::InvalidArgument* ecopy = new gum::InvalidArgument ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__InvalidArgument,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__InvalidArgument ), err );
} catch ( gum::Exception& e ) {
PyErr_SetString ( PyExc_Exception, e.errorContent().c_str() );
} catch ( std::exception& e ) {
PyErr_SetString ( PyExc_Exception, e.what() ); PyErr_SetString ( PyExc_Exception, e.what() );
} }
} }
......
...@@ -170,7 +170,7 @@ class TestFeatures(BayesNetTestCase): ...@@ -170,7 +170,7 @@ class TestFeatures(BayesNetTestCase):
bn.cpt("A").fillWith(1).normalize() bn.cpt("A").fillWith(1).normalize()
bn.generateCPT("B") bn.generateCPT("B")
bn.generateCPT("C") bn.generateCPT("C")
with self.assertRaises(IndexError): with self.assertRaises(gum.NotFound):
bn.cpt("XX") bn.cpt("XX")
bn.reverseArc("A", "C") bn.reverseArc("A", "C")
...@@ -180,19 +180,19 @@ class TestFeatures(BayesNetTestCase): ...@@ -180,19 +180,19 @@ class TestFeatures(BayesNetTestCase):
bn.reverseArc("A", "C") bn.reverseArc("A", "C")
with self.assertRaises(gum.GraphError): with self.assertRaises(gum.GraphError):
bn.reverseArc("A", "C") bn.reverseArc("A", "C")
with self.assertRaises(IndexError): with self.assertRaises(gum.NotFound):
bn.reverseArc("A", "X") bn.reverseArc("A", "X")
bn.erase("A") bn.erase("A")
self.assertEqual(str(bn), "BN{nodes: 2, arcs: 1, domainSize: 4, parameters: 6, compression ratio: -50% }") self.assertEqual(str(bn), "BN{nodes: 2, arcs: 1, domainSize: 4, parameters: 6, compression ratio: -50% }")
with self.assertRaises(IndexError): with self.assertRaises(gum.NotFound):
bn.erase("A") bn.erase("A")
bn.eraseArc("B", "C") bn.eraseArc("B", "C")
self.assertEqual(str(bn), "BN{nodes: 2, arcs: 0, domainSize: 4, parameters: 4, compression ratio: 0% }") self.assertEqual(str(bn), "BN{nodes: 2, arcs: 0, domainSize: 4, parameters: 4, compression ratio: 0% }")
with self.assertRaises(IndexError): with self.assertRaises(gum.NotFound):
bn.eraseArc("B", "C") bn.eraseArc("B", "C")
def testShortcutAdd(self): def testShortcutAdd(self):
......
...@@ -23,7 +23,7 @@ class TestInstantiation(pyAgrumTestCase): ...@@ -23,7 +23,7 @@ class TestInstantiation(pyAgrumTestCase):
with self.assertRaises(gum.OutOfBounds): with self.assertRaises(gum.OutOfBounds):
i['e']=1000 i['e']=1000
with self.assertRaises(IndexError): with self.assertRaises(gum.NotFound):
i['z']=0 i['z']=0
ts = unittest.TestSuite() ts = unittest.TestSuite()
......
...@@ -29,7 +29,7 @@ class LazyPropagationTestCase(pyAgrumTestCase): ...@@ -29,7 +29,7 @@ class LazyPropagationTestCase(pyAgrumTestCase):
with self.assertRaises(gum.InvalidArgument): with self.assertRaises(gum.InvalidArgument):
res = ie.evidenceImpact("visit_to_Asia?", ["visit_to_Asia?", "tuberculosis?", "tuberculos_or_cancer?"]) res = ie.evidenceImpact("visit_to_Asia?", ["visit_to_Asia?", "tuberculosis?", "tuberculos_or_cancer?"])
with self.assertRaises(IndexError): with self.assertRaises(gum.NotFound):
res = ie.evidenceImpact("visit_to_Asia?", ["toto", "tuberculosis?", "tuberculos_or_cancer?"]) res = ie.evidenceImpact("visit_to_Asia?", ["toto", "tuberculosis?", "tuberculos_or_cancer?"])
res = ie.evidenceImpact("visit_to_Asia?", ["tuberculosis?", "tuberculos_or_cancer?"]) res = ie.evidenceImpact("visit_to_Asia?", ["tuberculosis?", "tuberculos_or_cancer?"])
......
...@@ -238,6 +238,7 @@ ADD_APPROXIMATIONSCHEME_API(gum::learning::genericBNLearner,gum::learning::BNLea ...@@ -238,6 +238,7 @@ ADD_APPROXIMATIONSCHEME_API(gum::learning::genericBNLearner,gum::learning::BNLea
%exceptionclass gum::DefaultInLabel; %exceptionclass gum::DefaultInLabel;
%exceptionclass gum::Circuit; %exceptionclass gum::Circuit;
%exceptionclass gum::InvalidNode; %exceptionclass gum::InvalidNode;
%exceptionclass gum::NotFound;
%exceptionclass gum::InvalidEdge; %exceptionclass gum::InvalidEdge;
%exceptionclass gum::DuplicateLabel; %exceptionclass gum::DuplicateLabel;
%exceptionclass gum::DuplicateElement; %exceptionclass gum::DuplicateElement;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment