Exception stacktrace to a string?
843829Oct 5 2004 — edited Jan 20 2005Hi
My problem is that I want to get java exception stacktraces to a string using JNI calls in C.
I know how to do it with Java using Throwable.printStackTrace and so on, but I'm just wondering if it's valid to do it that way due to the somewhat unclear descriptions of the exception functions in JNI. My program is invoking java from C so there is no java function I will return to that can handle the exceptions at a higher level so I want to handle things from C.
I know I can get the exception using ExceptionDescribe, but then it says I can't use any other JNI calls until I use ExceptionClear (or ExceptionDescribe) to clear the exception. Thus my question is, can I use the jthrowable object the ExceptionDescribe returned after I call ExceptionClear, i.e. does the clear only clear the thrown status, but doesn't remove the actual throwable object? And if I can use it, I assume I have to release my reference to the throwable object useing DeleteLocalRef. Does it work this way or can't I use the throwable object at all in my code?
Example:
jthrowable err;
>>>JNI call that throws an exception
if ((err = (*env)->ExceptionOccurred(env)) != NULL)
{
(*env)->ExceptionClear(env);
>>>Function that use JNI to call java functions to print the exception stacktrace to a string
(*env)->DeleteLocalRef(env, err);
}
Hope someone can answer if this is possible or if I simply have to live without the stacktrace. The references and tutorials are a bit vague on this.