JNI exception capture

Source: Internet
Author: User

JNI exception capture
Http: // 192.210.60.138/DZ/Forum. php? MoD = viewthread & tid = 19 & fromuid = 1
(Source: istudy)

 

#define LOG_TAG "JNIHelp"#include "JNIHelp.h"#include "android/log.h"#include <string.h>#include <assert.h>#include <stdio.h>#include <stdlib.h>/* * Register native JNI-callable methods. * "className" looks like "java/lang/String" */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<jstring>(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<jstring>(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: <error getting message>", 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", "<error getting class name>");    }}/* 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; }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.