refaelos
refaelos

Reputation: 8075

JNI - catch exception and check its type

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 ?

Upvotes: 4

Views: 1874

Answers (1)

refaelos
refaelos

Reputation: 8075

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

Upvotes: 5

Related Questions