tags:

views:

326

answers:

1

Hello,

I have JNI layer in my application. In some cases JAVA throws an Exception. How can I get JAVA exception in JNI layer. I have the code something like as follows.

if((*(pConnDA->penv))->ExceptionCheck(pConnDA->penv)) { (*(pConnDA->penv))->ExceptionDescribe(pConnDA->penv); (*(pConnDA->penv))->ExceptionClear(pConnDA->penv); }

Is this block of code will catch only JNI exceptions? Where exception description will log in console(stderr)? How get this into buffer, so that i can pass to my logger module.

Thanks in Advance, Chandu

A: 

if you invoke a Java method from JNI, calling ExceptionCheck afterwards will return JNI_TRUE if an exception was thrown by the Java.

if you're just invoking a JNI function (such as FindClass), ExceptionCheck will tell you if that failed in a way that leaves a pending exception (as FindClass will do on error).

ExceptionDescribe outputs to stderr. there's no convenient way to make it go anywhere else, but ExceptionOccurred gives you a jthrowable if you want to play about with it, or you could just let it go up to Java and handle it there. that's the usual style:

jclass c = env->FindClass("class/does/not/Exist"); if (env->ExceptionCheck()) { return; } // otherwise do something with 'c'...

note that it doesn't matter what value you return; the calling Java code will never see it --- it'll see the pending exception instead.

Elliott Hughes