Skip to content

Commit

Permalink
Display Java stacktrace by default in JavaException (#696)
Browse files Browse the repository at this point in the history
* WIP - java exception printing

* try str instead

* change reporting of Java exception name to be more java-like

* use args[0] for the message

* address spacing
  • Loading branch information
cmacdonald authored Feb 6, 2025
1 parent ebef177 commit d539d9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions jnius/jnius_export_class.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class JavaException(Exception):
self.stacktrace = stacktrace
Exception.__init__(self, message)

def __str__(self):
'''
Override __str__ so that we can see the Java stacktrace
'''
rtr = self.args[0]
if self.stacktrace is not None:
rtr += '\n' + '\n\t'.join(self.stacktrace)
return rtr


cdef class JavaObject(object):
'''Can contain any Java object. Used to store instance, or whatever.
Expand Down
2 changes: 1 addition & 1 deletion jnius/jnius_utils.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ cdef void check_exception(JNIEnv *j_env) except *:
j_env[0].DeleteLocalRef(j_env, e_msg)
j_env[0].DeleteLocalRef(j_env, exc)

raise JavaException('JVM exception occurred: %s' % (pymsg + " " + str(pyexcclass) if pymsg is not None else pyexcclass), pyexcclass, pymsg, pystack)
raise JavaException('JVM exception occurred: %s' % (str(pyexcclass) + ": " + pymsg if pymsg is not None else pyexcclass), pyexcclass, pymsg, pystack)


cdef void _append_exception_trace_messages(
Expand Down

0 comments on commit d539d9e

Please sign in to comment.