JNI get exception object class name

J

Jaydeep Chovatia

Hi,

I have written an application in which i am calling java method from C+
+ using JNI(Java Native Interface). I would like to handle exceptions
in C++ raised inside JAVA method. I am able to do that successfully. I
also want class name of the exception object in C++ and for that I
have written following code, but line no. 9 retruns null object(pease
see code below).

1 jobject jResponseMailbox = jenv->CallStaticObjectMethod(...);
2 if(jenv->ExceptionCheck() == JNI_TRUE )
3 {
4 jthrowable exceptionObj = jenv->ExceptionOccurred();
5 jclass exceptionClass = jenv->GetObjectClass( exceptionObj );
6
7 jmethodID msgMethodID;
8 const char* localstr = NULL;
9 msgMethodID = jenv->GetMethodID(exceptionClass, "getName",
"()Ljava/lang/String;");
10 if(msgMethodID == NULL) {
11 printf("\ngetName is returning NULL......\n");
12 } else {
13 jstring clsName = (jstring)jenv-
CallObjectMethod(exceptionClass, msgMethodID, NULL);
14 localstr = jenv->GetStringUTFChars(clsName, NULL);
15 printf("\n\nException class name: %s\n", localstr);
16 }
17 }

Can you please suggest me how to get class name?

Thank you,
Jaydeep
 
E

EJP

I would be very careful about that. You're already in an exception, you
don't want to provoke another one by getting fancy. I would just either
use the built-in JNI functions to print the exception and stack trace or
let it be thrown back to the caller and let him handle it in Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top