From 6328de8142ae504c8a6a04a481a2316c463fc7ef Mon Sep 17 00:00:00 2001 From: Len Trigg Date: Fri, 16 May 2014 12:12:12 +1200 Subject: [PATCH] Incorporate patch from #96 and a unit test --- jnius/jnius_utils.pxi | 11 ++++++++++- tests/test_bad_declaration.py | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/jnius/jnius_utils.pxi b/jnius/jnius_utils.pxi index c2515282..7f473dc7 100644 --- a/jnius/jnius_utils.pxi +++ b/jnius/jnius_utils.pxi @@ -36,11 +36,20 @@ cdef parse_definition(definition): 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) - raise JavaException('JVM exception occured') + + 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, 'Ljava/lang/String;', e_string) + + raise JavaException('JVM exception occurred: ' + pystr) cdef dict assignable_from = {} diff --git a/tests/test_bad_declaration.py b/tests/test_bad_declaration.py index a7b46a71..c75b7867 100644 --- a/tests/test_bad_declaration.py +++ b/tests/test_bad_declaration.py @@ -21,3 +21,12 @@ def test_with_too_much_arguments(self): Stack = autoclass('java.util.Stack') stack = Stack() self.assertRaises(JavaException, stack.push, 'hello', 'world', 123) + + def test_java_exception_handling(self): + Stack = autoclass('java.util.Stack') + stack = Stack() + try: + stack.pop() + self.fail("Expected exception to be thrown") + except JavaException as je: + self.assertIn("EmptyStackException", str(je))