Skip to content

Commit

Permalink
throw EfelAssertionError to avoid exit(-1)
Browse files Browse the repository at this point in the history
  • Loading branch information
anilbey committed Oct 20, 2023
1 parent 31570e2 commit fd033a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion efel/cppcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions efel/cppcore/EfelExceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef EFEL_EXCEPTIONS_H
#define EFEL_EXCEPTIONS_H

#include <stdexcept>

// 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
6 changes: 4 additions & 2 deletions efel/cppcore/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <numeric>
#include <vector>
#include <utility>
#include "EfelExceptions.h"

using std::vector;

Expand Down Expand Up @@ -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);
}
}

Expand Down
11 changes: 9 additions & 2 deletions efel/cppcore/cppcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}


Expand Down

0 comments on commit fd033a2

Please sign in to comment.