From d539d9eac55cea1f1853316554a17d6213728fc9 Mon Sep 17 00:00:00 2001 From: Craig Macdonald Date: Thu, 6 Feb 2025 21:21:26 +0000 Subject: [PATCH] Display Java stacktrace by default in JavaException (#696) * 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 --- jnius/jnius_export_class.pxi | 9 +++++++++ jnius/jnius_utils.pxi | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/jnius/jnius_export_class.pxi b/jnius/jnius_export_class.pxi index a688e2b0..c7ab5adb 100644 --- a/jnius/jnius_export_class.pxi +++ b/jnius/jnius_export_class.pxi @@ -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. diff --git a/jnius/jnius_utils.pxi b/jnius/jnius_utils.pxi index ef5d6ab9..381d4b05 100644 --- a/jnius/jnius_utils.pxi +++ b/jnius/jnius_utils.pxi @@ -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(