RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
JNI捕获异常

jnihelp.h头文件根据需求自行添加,也可以直接删除

站在用户的角度思考问题,与客户深入沟通,找到海南网站设计与海南网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖海南地区。

#define LOG_TAG "JNIHelp"
#include "JNIHelp.h"
#include "android/log.h"
#include
#include
#include
#include


static void getExceptionSummary(JNIEnv* env, jthrowable exception, char* buf, size_t bufLen)
{
    int success = 0;

    /* get the name of the exception's class */
    jclass exceptionClazz = env->GetObjectClass(exception); // can't fail
    jclass classClazz = env->GetObjectClass(exceptionClazz); // java.lang.Class, can't fail
    jmethodID classGetNameMethod = env->GetMethodID(classClazz, "getName", "()Ljava/lang/String;");
    jstring classNameStr = static_cast(env->CallObjectMethod(exceptionClazz, classGetNameMethod));
    if (classNameStr != NULL) {
        /* get printable string */
        const char* classNameChars = env->GetStringUTFChars( classNameStr, NULL);
        if (classNameChars != NULL) {
            /* if the exception has a message string, get that */
            jmethodID throwableGetMessageMethod = env->GetMethodID(
                    exceptionClazz, "getMessage", "()Ljava/lang/String;");
            jstring messageStr = static_cast(env->CallObjectMethod(exception, throwableGetMessageMethod));

            if (messageStr != NULL) {
                const char* messageChars = env->GetStringUTFChars( messageStr, NULL);
                if (messageChars != NULL) {
                    snprintf(buf, bufLen, "%s: %s", classNameChars, messageChars);
                    env->ReleaseStringUTFChars( messageStr, messageChars);
                } else {
                    env->ExceptionClear(); // clear OOM
                    snprintf(buf, bufLen, "%s: ", classNameChars);
                }
                env->DeleteLocalRef( messageStr);
            } else {
                strncpy(buf, classNameChars, bufLen);
                buf[bufLen - 1] = '\0';
            }

            env->ReleaseStringUTFChars( classNameStr, classNameChars);
            success = 1;
        }
        env->DeleteLocalRef( classNameStr);
    }
    env->DeleteLocalRef( classClazz);
    env->DeleteLocalRef( exceptionClazz);

    if (! success) {
        env->ExceptionClear();
        snprintf(buf, bufLen, "%s", "");
    }
}
/* Throw an exception with the specified class and an optional message.*/
int jniThrowException(JNIEnv* env)
{
     jclass exceptionClass;
     int result = 0;

     if (env->ExceptionCheck()) {
         /* TODO: consider creating the new exception with this as "cause" */
         char buf[256];

         jthrowable exception = env->ExceptionOccurred();
         env->ExceptionClear();
         if (exception != NULL) {
             getExceptionSummary(env,exception, buf, sizeof(buf));
             //LOGW("Discarding pending exception (%s) to throw %s\n", buf, className);
             __android_log_print(ANDROID_LOG_INFO, "", "Discarding pending exception (%s) to throw\n",buf);
             env->DeleteLocalRef(exception);
             result = -1;
         }
     }
     return result;
}

分享题目:JNI捕获异常
网站链接:http://sczitong.cn/article/pdjsch.html