How do i check if a java method throws an exception in my C/C++ JNI code?

Davanum Srinivas

Use ExceptionOccurred as shown below:

 * Class:     JNIExample
 * Method:    putMeInVector
 * Signature: ()Ljava/util/Vector;
JNIEXPORT jobject JNICALL Java_JNIExample_putMeInVector
  (JNIEnv * env, jobject self)
    // Construct new Vector
    jclass clazz = env->FindClass("java/util/Vector");
    if (env->ExceptionOccurred()) return 0;
    jmethodID midCtor = env->GetMethodID(clazz, "<init>", "()V");
    if (env->ExceptionOccurred()) return 0;
    jobject vector = env->NewObject(clazz, midCtor);
    if (env->ExceptionOccurred()) return 0;
    // Put me in the vector
    jmethodID midAddElement = env->GetMethodID(clazz, "addElement", "(Ljava/lang/Object;)V");
    if (env->ExceptionOccurred()) return 0;
    env->CallVoidMethod(vector, midAddElement, self);
    if (env->ExceptionOccurred()) return 0;

    // Return vector as result of method
    return vector;
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact