Skip to content

Include originating function in C-API catch-all exception message#29561

Open
GopalakrishnanN wants to merge 3 commits into
mainfrom
GopalakrishnanN/capi-unknown-exception-context
Open

Include originating function in C-API catch-all exception message#29561
GopalakrishnanN wants to merge 3 commits into
mainfrom
GopalakrishnanN/capi-unknown-exception-context

Conversation

@GopalakrishnanN

Copy link
Copy Markdown
Contributor

Description

The terminal catch (...) handler in the API_IMPL_END macro returned ORT_FAIL with the literal string "Unknown Exception":

catch (...) {
  return OrtApis::CreateStatus(ORT_FAIL, "Unknown Exception");
}

For any non-std::exception throwable that crosses the C ABI (rare, but e.g. a thrown integer from third-party code), this discarded all context, including which C-API function it surfaced in. This change records the originating function via MakeString(..., __func__):

catch (...) {
  return OrtApis::CreateStatus(ORT_FAIL, ::onnxruntime::MakeString("Unknown exception in ", __func__).c_str());
}

API_IMPL_END expands at the end of each C-API function body, so __func__ resolves to the function where the unknown exception was caught. MakeString is already available (error_code_helper.h includes core/common/make_string.h), and CreateStatus copies the message, so the temporary is safe.

Only the catch-all diagnostic message changes. The OnnxRuntimeException / NotImplementedException / std::exception handlers and all normal execution paths are unaffected.

Motivation and Context

Small diagnostic improvement flagged in an error-handling review (item EH3): the catch-all funnel collapsed to a context-free string, making a non-std::exception failure that reaches the C ABI hard to localize. Naming the originating function turns "something threw somewhere" into "something threw in OrtApiName".

Testing

Built onnxruntime_test_all (Release, Windows) — compiles cleanly across the C-API translation units that use API_IMPL_END; InferenceSessionTests smoke test passes on the rebuilt binary.

Note: the catch (...) path fires only for a non-std::exception throwable, which cannot be triggered from a unit test without editing production code, so this is a compile-verified diagnostic-only change.

Gopalakrishnan Nallasamy and others added 3 commits July 5, 2026 21:43
The terminal 'catch (...)' in API_IMPL_END returned ORT_FAIL with the literal 'Unknown Exception', discarding all context for any non-std::exception throwable that crosses the C ABI. Include the originating C-API function name via MakeString(..., __func__) so the returned OrtStatus points at where the unknown exception surfaced. Only the catch-all diagnostic message changes; all other handlers and normal paths are unaffected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant