JNI - catch exception and check its type

android, exception, java, java-native-interface

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();
  }
}

Problem

I'm looking for a way to catch an exception that was thrown on the Android (JAVA) side and handle it on the Native side. What i need to do is to detect the type of exception and handle it accordingly. Any idea how it's done ?

Original source