diff --git a/efel/cppcore/CMakeLists.txt b/efel/cppcore/CMakeLists.txt index 053cb3bc..bbd8e61c 100644 --- a/efel/cppcore/CMakeLists.txt +++ b/efel/cppcore/CMakeLists.txt @@ -40,6 +40,6 @@ add_library(efel SHARED ${FEATURESRCS}) install(TARGETS efel LIBRARY DESTINATION lib) install(FILES efel.h cfeature.h FillFptrTable.h LibV1.h LibV2.h LibV3.h - LibV5.h mapoperations.h Utils.h DependencyTree.h eFELLogger.h + LibV5.h mapoperations.h Utils.h DependencyTree.h eFELLogger.h EfelExceptions.h types.h DESTINATION include) diff --git a/efel/cppcore/EfelExceptions.h b/efel/cppcore/EfelExceptions.h new file mode 100644 index 00000000..ac7c85c2 --- /dev/null +++ b/efel/cppcore/EfelExceptions.h @@ -0,0 +1,12 @@ +#ifndef EFEL_EXCEPTIONS_H +#define EFEL_EXCEPTIONS_H + +#include + +// Define the custom exception class +class EfelAssertionError : public std::runtime_error { +public: + explicit EfelAssertionError(const std::string& message) : std::runtime_error(message) {} +}; + +#endif // EFEL_EXCEPTIONS_H diff --git a/efel/cppcore/Utils.h b/efel/cppcore/Utils.h index 5e59ba6a..c064cb6a 100644 --- a/efel/cppcore/Utils.h +++ b/efel/cppcore/Utils.h @@ -26,6 +26,7 @@ #include #include #include +#include "EfelExceptions.h" using std::vector; @@ -85,8 +86,9 @@ inline void efel_assert(bool assertion, const char *message, const char *file, const int line) { if(!assertion){ - printf("Assertion fired(%s:%d): %s\n", file, line, message); - exit(-1); + using std::string, std::to_string; + string errorMsg = "Assertion fired(" + string(file) + ":" + to_string(line) + "): " + string(message); + throw EfelAssertionError(errorMsg); } } diff --git a/efel/cppcore/cppcore.cpp b/efel/cppcore/cppcore.cpp index e9a6471a..196057d4 100644 --- a/efel/cppcore/cppcore.cpp +++ b/efel/cppcore/cppcore.cpp @@ -118,8 +118,7 @@ _getfeature(PyObject* self, PyObject* args, const string &input_type) { } string feature_type; - try - { + try { feature_type = pFeature->featuretype(string(feature_name)); } catch(const std::runtime_error& e) @@ -146,10 +145,18 @@ _getfeature(PyObject* self, PyObject* args, const string &input_type) { return Py_BuildValue("i", return_value); } } + catch(EfelAssertionError& e) { // more specialised exception + PyErr_SetString(PyExc_AssertionError, e.what()); + return NULL; + } catch(const std::runtime_error& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return NULL; } + catch(...) { + PyErr_SetString(PyExc_RuntimeError, "Unknown error"); + return NULL; + } }