Skip to content

Commit

Permalink
Incorporate patch from kivy#96 and a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenbok committed May 16, 2014
1 parent 19d4ddc commit 6328de8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion jnius/jnius_utils.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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, <bytes> 'Ljava/lang/String;', e_string)

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


cdef dict assignable_from = {}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_bad_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 6328de8

Please sign in to comment.