diff --git a/include/mxnet/c_api_error.h b/include/mxnet/c_api_error.h index 0c6ea03fa459..e76a2c99f8d3 100644 --- a/include/mxnet/c_api_error.h +++ b/include/mxnet/c_api_error.h @@ -31,9 +31,26 @@ * and finishes with API_END() or API_END_HANDLE_ERROR() * The finally clause contains procedure to cleanup states when an error happens. */ -#define MX_API_BEGIN() try { on_enter_api(__FUNCTION__); -#define MX_API_END() } catch(dmlc::Error &_except_) { on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*) -#define MX_API_END_HANDLE_ERROR(Finalize) } catch(dmlc::Error &_except_) { Finalize; on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*) +#define MX_API_BEGIN() \ + try { \ + on_enter_api(__FUNCTION__); +#define MX_API_END() \ + } \ + catch (const std::exception &_except_) { \ + on_exit_api(); \ + return MXAPIHandleException(_except_); \ + } \ + on_exit_api(); \ + return 0; // NOLINT(*) +#define MX_API_END_HANDLE_ERROR(Finalize) \ + } \ + catch (const std::exception &_except_) { \ + Finalize; \ + on_exit_api(); \ + return MXAPIHandleException(_except_); \ + } \ + on_exit_api(); \ + return 0; // NOLINT(*) /*! * \brief Set the last error message needed by C API * \param msg The error message to set. @@ -44,7 +61,7 @@ void MXAPISetLastError(const char* msg); * \param e the exception * \return the return value of API after exception is handled */ -inline int MXAPIHandleException(const dmlc::Error &e) { +inline int MXAPIHandleException(const std::exception &e) { MXAPISetLastError(e.what()); return -1; } diff --git a/src/engine/threaded_engine.h b/src/engine/threaded_engine.h index 3d2119d63291..7cf181ec3781 100644 --- a/src/engine/threaded_engine.h +++ b/src/engine/threaded_engine.h @@ -380,7 +380,7 @@ class ThreadedEngine : public Engine { } else { callback(); } - } catch (dmlc::Error& e) { + } catch (const std::exception& e) { threaded_opr->opr_exception = std::make_shared(std::current_exception()); callback(); diff --git a/tests/python/unittest/test_exc_handling.py b/tests/python/unittest/test_exc_handling.py index 5627ac50d26e..960a4e8d1364 100644 --- a/tests/python/unittest/test_exc_handling.py +++ b/tests/python/unittest/test_exc_handling.py @@ -166,9 +166,8 @@ def test_multiple_waitalls(): mx.nd.waitall() @with_seed() -def test_exc_profiler(): - def run_training_iteration(data): - output = net(data) +def run_training_iteration(data): + output = net(data) net = gluon.nn.HybridSequential() with net.name_scope(): @@ -182,6 +181,14 @@ def run_training_iteration(data): mx.nd.waitall() mx.profiler.set_state("stop") +@with_seed() +def test_opencv_exception(): + def check_resize(): + img = mx.nd.ones((1200, 1600, 3)) + img = mx.image.imresize(img, 320, 320, interp=-1) + img.asnumpy() + assert_raises(MXNetError, check_resize) + if __name__ == '__main__': import nose