在android的JNIHelp.h文件中声明四种可以向JVM抛异常的函数:

  1. int jniThrowException(JNIEnv* env, const char* className,const char* msg)
  2. int jniThrowNullPointerException(JNIEnv* env, char* msg)
  3. int jniThrowIOException(JNIEnv* env, int errnum)
  4. int jniThrowRuntimeException(JNIEnv* env, const char* msg)
注意虽然 const char* className它是字符串,但是它是要传到中使用,所以它必须和某个类相对应的。
另外注意,这里的抛异常函数并不是在抛异常的同时退出当前函数,而是在函数最终返回时,才真正的抛出异常。
使用时记得添加头文件:

#include <jni.h>
#include <JNIHelp.h>
#include <android_runtime/AndroidRuntime.h>

static void
android_media_MediaScanner_setLocale(JNIEnv *env, jobject thiz, jstring locale)
{
    MediaScanner *mp = (MediaScanner *)env->GetIntField(thiz, fields.context);

    if (locale == NULL) {
        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
        return;
    }

相关文章:

  • 2022-12-23
  • 2020-12-23
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
猜你喜欢
  • 2021-07-13
  • 2021-12-18
  • 2022-02-10
  • 2022-12-23
相关资源
相似解决方案