onnxruntime: Provide a way to handle errors in the C++ API when exceptions are disabled
I’m using ONNX Runtime as a shared library inside a project where exceptions are disabled and enabling them is not an option. So, to avoid build errors from the throw statements I define the macro ORT_NO_EXCEPTIONS. However, doing so causes std::abort() to be called on error.
Now let’s say that, as part of some other bigger process, I want to create an Ort::Session to check if a model file is fine and extract its metadata. If things go wrong, I just want to say “nope, this didn’t work” and carry on. However, because exceptions are disabled, the current API provides no way to handle the error if there is a problem. The entire process just dies from the call to std::abort().
Would it be possible to provide a way to handle errors in the C++ API when exceptions are disabled?
System information
- ONNX Runtime version (you are using): v1.10.0
Describe the solution you’d like I’d like to have some way to get an OrtStatus I can use to handle errors instead of an automatic std::abort() when exceptions are disabled. Ideally this would apply to any C++ APIs that currently have this throw on error behavior, but at the very least I’d like it when initializing Ort::Session objects and running them.
Describe alternatives you’ve considered
I’ve tried using the underlying APIs that Ort::Session constructors call to handle the error, but the p_ pointer is declared as protected and cannot be accessed externally. This forces me out of the Ort::Session API.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 15 (15 by maintainers)
Commits related to this issue
- Move #ifndef ORT_CXX_API_THROW to the no exceptions case. This is related to https://github.com/microsoft/onnxruntime/issues/10564 which introduced a fix in the wrong case where exceptions are enable... — committed to leandro-gracia-gil/onnxruntime by leandro-gracia-gil 2 years ago
- Move #ifndef ORT_CXX_API_THROW to the no exceptions case. (#10937) This is related to https://github.com/microsoft/onnxruntime/issues/10564 which introduced a fix in the wrong case where exceptions ... — committed to microsoft/onnxruntime by leandro-gracia-gil 2 years ago
- Move #ifndef ORT_CXX_API_THROW to the no exceptions case. (#10937) This is related to https://github.com/microsoft/onnxruntime/issues/10564 which introduced a fix in the wrong case where exceptions ... — committed to intel/onnxruntime by leandro-gracia-gil 2 years ago
- Move #ifndef ORT_CXX_API_THROW to the no exceptions case. (#10937) This is related to https://github.com/microsoft/onnxruntime/issues/10564 which introduced a fix in the wrong case where exceptions ... — committed to seddonm1/onnxruntime by leandro-gracia-gil 2 years ago
@RyanUnderhill Sorry to reopen this, but the way the patch is implemented is in fact preventing me from defining my own
ORT_CXX_API_THROW.The issue is that I also define
ORT_NO_EXCEPTIONS(which is the very reason I need to customize error management, because I cannot use exceptions), but as it is now you can only redefineORT_CXX_API_THROWwhenORT_NO_EXCEPTIONSis not defined.This is how it looks like now.
Instead, it should be something like this:
Or alternatively, allow redefinition in both cases if you think that’s more appropriate. My use case only needs to allow redefinition when
ORT_NO_EXCEPTIONSis defined.