[pyAgrum] Better exception handling

parent e91a95d8
......@@ -36,22 +36,12 @@
#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
#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) \
{ \
std::ostringstream __error__str; \
__error__str << msg; \
throw(type(__error__str.str())); \
}
#endif // NDEBUG
#define GUM_SHOWERROR(e) \
{ \
std::cout << std::endl \
......@@ -59,7 +49,6 @@
}
#else
#ifdef NDEBUG
#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
#define GUM_ERROR(type, msg) \
{ \
std::ostringstream __error__str; \
......@@ -74,7 +63,6 @@
<< (e).errorContent() << std::endl; \
}
#else
#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
#define GUM_ERROR(type, msg) \
{ \
std::ostringstream __error__str; \
......@@ -98,6 +86,8 @@
public: \
TYPE(std::string aMsg, std::string aType = MSG) \
: SUPERCLASS(aMsg, aType){}; \
TYPE(const TYPE& src) \
: SUPERCLASS(src){}; \
};
#define GUM_SYNTAX_ERROR(msg, line, column) \
......@@ -131,9 +121,12 @@ namespace gum {
~Exception() {}
/// @}
const std::string toString() const { return _msg; }
/// @}
#ifdef SWIG
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.
......
/* 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() {
try {
throw;
} catch ( std::bad_cast& e ) {
}
catch ( std::bad_cast& e ) {
PyErr_SetString ( PyExc_RuntimeError, "C++ Bad Cast" );
} catch ( gum::NotFound& e ) {
PyErr_SetString ( PyExc_IndexError, e.errorContent().c_str() );
} catch ( gum::DefaultInLabel& e ) {
gum::DefaultInLabel* ecopy = new gum::DefaultInLabel ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__DefaultInLabel,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__DefaultInLabel ), err );
} catch ( gum::IdError& e ) {
gum::IdError* ecopy = new gum::IdError ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__IdError,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__IdError ), err );
} catch ( gum::IOError& e ) {
gum::IOError* ecopy = new gum::IOError ( e );
PyObject* err = SWIG_NewPointerObj ( ecopy,
SWIGTYPE_p_gum__IOError,
1 );
PyErr_SetObject (
SWIG_Python_ExceptionType ( SWIGTYPE_p_gum__IOError ),
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 ) {
}
PYGUM_CATCH(NotFound)
PYGUM_CATCH(DefaultInLabel)
PYGUM_CATCH(IdError)
PYGUM_CATCH(IOError)
PYGUM_CATCH(OperationNotAllowed)
PYGUM_CATCH(InvalidDirectedCycle)
PYGUM_CATCH(InvalidNode)
PYGUM_CATCH(InvalidArc)
PYGUM_CATCH(InvalidEdge)
PYGUM_CATCH(DuplicateLabel)
PYGUM_CATCH(DuplicateElement)
PYGUM_CATCH(OutOfLowerBound)
PYGUM_CATCH(OutOfUpperBound)
PYGUM_CATCH(OutOfBounds)
PYGUM_CATCH(UnknownLabelInDatabase)
PYGUM_CATCH(FatalError)
PYGUM_CATCH(UndefinedElement)
PYGUM_CATCH(InvalidArgument)
catch ( gum::Exception& e ) {
PyErr_SetString ( PyExc_Exception, e.what().c_str() );
}
catch ( std::exception& e ) {
PyErr_SetString ( PyExc_Exception, e.what() );
}
}
......
......@@ -170,7 +170,7 @@ class TestFeatures(BayesNetTestCase):
bn.cpt("A").fillWith(1).normalize()
bn.generateCPT("B")
bn.generateCPT("C")
with self.assertRaises(IndexError):
with self.assertRaises(gum.NotFound):
bn.cpt("XX")
bn.reverseArc("A", "C")
......@@ -180,19 +180,19 @@ class TestFeatures(BayesNetTestCase):
bn.reverseArc("A", "C")
with self.assertRaises(gum.GraphError):
bn.reverseArc("A", "C")
with self.assertRaises(IndexError):
with self.assertRaises(gum.NotFound):
bn.reverseArc("A", "X")
bn.erase("A")
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.eraseArc("B", "C")
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")
def testShortcutAdd(self):
......
......@@ -23,7 +23,7 @@ class TestInstantiation(pyAgrumTestCase):
with self.assertRaises(gum.OutOfBounds):
i['e']=1000
with self.assertRaises(IndexError):
with self.assertRaises(gum.NotFound):
i['z']=0
ts = unittest.TestSuite()
......
......@@ -29,7 +29,7 @@ class LazyPropagationTestCase(pyAgrumTestCase):
with self.assertRaises(gum.InvalidArgument):
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?", ["tuberculosis?", "tuberculos_or_cancer?"])
......
......@@ -238,6 +238,7 @@ ADD_APPROXIMATIONSCHEME_API(gum::learning::genericBNLearner,gum::learning::BNLea
%exceptionclass gum::DefaultInLabel;
%exceptionclass gum::Circuit;
%exceptionclass gum::InvalidNode;
%exceptionclass gum::NotFound;
%exceptionclass gum::InvalidEdge;
%exceptionclass gum::DuplicateLabel;
%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