how to show C line number AND exception type in JNI exception stacktrace?


E

Elaine Cheong

Does anyone know how to display BOTH the C code line number and the Java
exception type in a JNI exception stack trace in Java? Printing to stdout
or stderr in C is not sufficient, since I need to display the information
in a Java GUI dialog box.

Example:
In my C code, I call JNI GetMethodID() to get a reference to a Java method
that doesn't exist. NoSuchMethodError exception is thrown. I am able to
do one of the following:

a. In the C code, check for the exception, and return immediately to the
Java code, where it catches the NoSuchMethodError Throwable object and
displays the stack trace, which only indicates the Java code line numbers
and type of the Throwable object. It does not indicate the C code line
number(s).

b. In the C code, check for the exception, print out the stack trace
using ExceptionDescribe(), clear the exception using ExceptionClear(), and
then creating and throwing my own Throwable object that contains the C
code line number (using __LINE__) in the error message back to the Java
code. Is it possible to get the original Throwable object type and error
message saved into a C data structure w/o crashing the JVM due to calling
disallowed JNI functions? Otherwise, the Java code does not know which
one of the following exceptions was thrown:

- NoSuchMethodError: if the specified method cannot be found.
- ExceptionInInitializerError: if the class initializer fails due to an
exception.
- OutOfMemoryError: if the system runs out of memory.

If you post to the newsgroup, please also reply to me by email.

Thanks,
Elaine
 
Ad

Advertisements

G

Gordon Beaton

a. In the C code, check for the exception, and return immediately
to the Java code, where it catches the NoSuchMethodError Throwable
object and displays the stack trace, which only indicates the Java
code line numbers and type of the Throwable object. It does not
indicate the C code line number(s).

After detecting the exception in your native code, store the line
number and other state that might be interesting, then return to Java.

In your catch block, call a native method to retrieve the stored
information, then throw a new exception containing the line number and
the original exception.

I agree it would be nice if you could catch the exception in C!
If you post to the newsgroup, please also reply to me by email.

I don't do mail, sorry.

/gordon
 

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

Top