How do i check if a java method throws an exception in my C/C++ JNI code?
Created May 4, 2012
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; }