Java – JNI – Catch exceptions and check their types

JNI – Catch exceptions and check their types… here is a solution to the problem.

JNI – Catch exceptions and check their types

I’m looking for a way to catch the exception thrown on the Android (JAVA) side and handle it on the native side.

What I need to do is detect the type of exception and deal with it accordingly.

Any idea how it is done?

Solution

I figured it out….

if(jEnv->ExceptionCheck() == JNI_TRUE ) {
  __android_log_write(ANDROID_LOG_DEBUG, "JNI", "HAS EXCEPTION"); 
  jthrowable exceptionObj = jEnv->ExceptionOccurred();

jclass exceptionClass = cocos2d::JniHelper::getClassID("com/companyName/example/exceptions/MyException", jEnv);
  if (jEnv->IsInstanceOf(exceptionObj, exceptionClass)) {
    __android_log_write(ANDROID_LOG_DEBUG, "JNI", "Cought MyException!"); 

throw MyException();
  }
}

Related Problems and Solutions