Android: Ort::Exception while loading a corrupt model file terminates the process instead of throwing a Java exception

Author: paoloantinoriCreated Sep 25, 2026Updated Sep 25, 2026

Version: sherpa-onnx 1.13.8, Android AAR (arm64), observed in production on Android 16.

When OfflineRecognizer.newFromFile receives a model file that fails to parse (in our case an encoder .onnx corrupted on disk after a verified download; onnxruntime reports: Load model from ... failed: Protobuf parsing failed), the Ort::Exception escapes the JNI boundary and the process terminates:

terminating due to uncaught exception of type Ort::Exception #00 abort (libc) #01..#10 libsherpa-onnx-jni.so #11 art_jni_trampoline #13 com.k2fsa.sherpa.onnx.OfflineRecognizer.

The Java side cannot catch this: the try/catch around the constructor never sees it, so one corrupt file kills the app on every load attempt.

I checked the JNI sources for OfflineRecognizer_newFromFile in jni/offline-recognizer.cc: the config validation is wrapped, but the recognizer construction path lets C++ exceptions propagate past the boundary. Other constructors in the Android API surface look like the same pattern.

Request: catch Ort::Exception (and any C++ exception) at the JNI boundary and convert it to a Java exception (env->ThrowNew or the pending-exception pattern used elsewhere in the codebase), so a bad model file becomes a catchable Java error. Happy to provide our full native stack if useful.

Context: I maintain an Android app (Anti-Vocale) shipping sherpa-onnx onnxruntime-based models; we verify SHA-256 at download time, but a file corrupted later on flash storage takes the whole app down.