Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include JVM exception details in python JavaException #96

Closed
eugfeld opened this issue Jan 15, 2014 · 3 comments
Closed

Include JVM exception details in python JavaException #96

eugfeld opened this issue Jan 15, 2014 · 3 comments

Comments

@eugfeld
Copy link

eugfeld commented Jan 15, 2014

Pyjnius throws JavaException when it detects a JVM exception. However python's JavaException does not contain any information about JVM exception that caused it. Even though pyjnius invokes JNI->ExceptionDescribe (which prints it to stderr), still at times it is hard to correlate and it would be a lot more informative to actually include at least JVM exception message in python exception.

In my local shadow I have the following code (jnius_utils.py):

cdef void check_exception(JNIEnv *j_env) except *:

cdef jmethodID toString = NULL
cdef jstring e_string
cdef jboolean isCopy

cdef jthrowable exc = j_env[0].ExceptionOccurred(j_env)
if exc:
    j_env[0].ExceptionDescribe(j_env)
    j_env[0].ExceptionClear(j_env)

    toString = j_env[0].GetMethodID(j_env, j_env[0].FindClass(j_env, "java/lang/Object"), "toString", "()Ljava/lang/String;");
    e_string = j_env[0].CallObjectMethod(j_env, exc, toString);

    pystr = convert_jobject_to_python(
                    j_env, <bytes> 'Ljava/lang/String;', e_string)

    raise JavaException('JVM exception occured: ' + pystr)

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@jtoledo1974
Copy link

I would also find the patch useful

Lenbok added a commit to Lenbok/pyjnius that referenced this issue May 18, 2014
@clintharris
Copy link

+1 for merging this patch. Exceptions have meaning; not being able to properly handle them is a significant issue/shortfall with PyJNIus.

@KeyWeeUsr
Copy link
Contributor

Closed by #115.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants