From 37a1bb90d91554c63064209b12b12bcbe33fdb3c Mon Sep 17 00:00:00 2001 From: Ivan Provalov Date: Mon, 21 Oct 2024 11:30:07 -0700 Subject: [PATCH 1/2] JNI Exception Handling JNI Exception Handling --- sherpa-onnx/jni/offline-recognizer.cc | 16 ++++++++---- sherpa-onnx/jni/voice-activity-detector.cc | 30 ++++++++++++++++------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/sherpa-onnx/jni/offline-recognizer.cc b/sherpa-onnx/jni/offline-recognizer.cc index 5e4b359b6..dc27fe73d 100644 --- a/sherpa-onnx/jni/offline-recognizer.cc +++ b/sherpa-onnx/jni/offline-recognizer.cc @@ -300,11 +300,17 @@ Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_createStream(JNIEnv * /*env*/, SHERPA_ONNX_EXTERN_C JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_decode( - JNIEnv * /*env*/, jobject /*obj*/, jlong ptr, jlong streamPtr) { - auto recognizer = reinterpret_cast(ptr); - auto stream = reinterpret_cast(streamPtr); - - recognizer->DecodeStream(stream); + JNIEnv *env, jobject /*obj*/, jlong ptr, jlong streamPtr) { + try { + auto recognizer = reinterpret_cast(ptr); + auto stream = reinterpret_cast(streamPtr); + recognizer->DecodeStream(stream); + } catch (const std::exception& e) { + jclass exClass = env->FindClass("java/lang/RuntimeException"); + if (exClass != nullptr) { + env->ThrowNew(exClass, e.what()); + } + } } SHERPA_ONNX_EXTERN_C diff --git a/sherpa-onnx/jni/voice-activity-detector.cc b/sherpa-onnx/jni/voice-activity-detector.cc index a30423f70..5e0628574 100644 --- a/sherpa-onnx/jni/voice-activity-detector.cc +++ b/sherpa-onnx/jni/voice-activity-detector.cc @@ -112,14 +112,21 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_delete(JNIEnv * /*env*/, SHERPA_ONNX_EXTERN_C JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_acceptWaveform( JNIEnv *env, jobject /*obj*/, jlong ptr, jfloatArray samples) { - auto model = reinterpret_cast(ptr); + try { + auto model = reinterpret_cast(ptr); - jfloat *p = env->GetFloatArrayElements(samples, nullptr); - jsize n = env->GetArrayLength(samples); + jfloat *p = env->GetFloatArrayElements(samples, nullptr); + jsize n = env->GetArrayLength(samples); - model->AcceptWaveform(p, n); + model->AcceptWaveform(p, n); - env->ReleaseFloatArrayElements(samples, p, JNI_ABORT); + env->ReleaseFloatArrayElements(samples, p, JNI_ABORT); + } catch (const std::exception& e) { + jclass exClass = env->FindClass("java/lang/RuntimeException"); + if (exClass != nullptr) { + env->ThrowNew(exClass, e.what()); + } + } } SHERPA_ONNX_EXTERN_C @@ -173,11 +180,18 @@ JNIEXPORT bool JNICALL Java_com_k2fsa_sherpa_onnx_Vad_isSpeechDetected( } SHERPA_ONNX_EXTERN_C -JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_reset(JNIEnv * /*env*/, +JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_reset(JNIEnv *env, jobject /*obj*/, jlong ptr) { - auto model = reinterpret_cast(ptr); - model->Reset(); + try { + auto model = reinterpret_cast(ptr); + model->Reset(); + } catch (const std::exception& e) { + jclass exClass = env->FindClass("java/lang/RuntimeException"); + if (exClass != nullptr) { + env->ThrowNew(exClass, e.what()); + } + } } SHERPA_ONNX_EXTERN_C From 143f585295ab5462d2803ff37d51dac3056f55de Mon Sep 17 00:00:00 2001 From: Ivan Provalov Date: Thu, 24 Oct 2024 15:13:37 -0700 Subject: [PATCH 2/2] Catching the most generic error Catching the most generic error --- sherpa-onnx/jni/offline-recognizer.cc | 5 +++++ sherpa-onnx/jni/voice-activity-detector.cc | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/sherpa-onnx/jni/offline-recognizer.cc b/sherpa-onnx/jni/offline-recognizer.cc index dc27fe73d..82bd7ffed 100644 --- a/sherpa-onnx/jni/offline-recognizer.cc +++ b/sherpa-onnx/jni/offline-recognizer.cc @@ -310,6 +310,11 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_decode( if (exClass != nullptr) { env->ThrowNew(exClass, e.what()); } + } catch (...) { + jclass exClass = env->FindClass("java/lang/RuntimeException"); + if (exClass != nullptr) { + env->ThrowNew(exClass, "Native exception: caught unknown exception"); + } } } diff --git a/sherpa-onnx/jni/voice-activity-detector.cc b/sherpa-onnx/jni/voice-activity-detector.cc index 5e0628574..9459ba846 100644 --- a/sherpa-onnx/jni/voice-activity-detector.cc +++ b/sherpa-onnx/jni/voice-activity-detector.cc @@ -126,6 +126,11 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_acceptWaveform( if (exClass != nullptr) { env->ThrowNew(exClass, e.what()); } + } catch (...) { + jclass exClass = env->FindClass("java/lang/RuntimeException"); + if (exClass != nullptr) { + env->ThrowNew(exClass, "Native exception: caught unknown exception"); + } } } @@ -191,6 +196,11 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_reset(JNIEnv *env, if (exClass != nullptr) { env->ThrowNew(exClass, e.what()); } + } catch (...) { + jclass exClass = env->FindClass("java/lang/RuntimeException"); + if (exClass != nullptr) { + env->ThrowNew(exClass, "Native exception: caught unknown exception"); + } } }