rust jni对Java异常的处理

在CPP中,可以调用 ExceptionOccurred 和 ExceptionCheck 来检查处理异常

jthrowable flag = env->ExceptionOccurred();
{
   /* Handle exception here or free up any resources held
      Exception remains pending until control returns back
      to the Java code.
   */
   return;
}

jboolean flag = env->ExceptionCheck();
if (flag) {
   /* Handle exception here or free up any resources held
      Exception remains pending until control returns back
      to the Java code.
   */
   env->ExceptionClear();



   return;
}

在 rust 的 jni 这个crate里, 上述两个函数,被转换成了 exception_occurred 和 exception_check

在rust调用 call_static_method 或者 call_method, 都不会执行异常处理,需要你自己 处理它们

let result = je.call_static_method(cls, "renderTo", "(II[B)V", &[
    JValue::from(width),
    JValue::from(height),
    JValue::from(rgbs2),
])?;
je.exception_check()?;

在调用 可能抛出Java异常的JNI函数后,总是调用 ExceptionCheck 或 ExceptionOccurred 来检查下异常。
总是检查从 JNI函数返回的值。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注