Skip to content

Commit c150c85

Browse files
committed
refactor: create function for exception toString handling
1 parent 4b72834 commit c150c85

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/core.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,22 +496,33 @@ javaclassname(class::Symbol) = replace(string(class), "."=>"/")
496496
javaclassname(class::AbstractString) = replace(class, "."=>"/")
497497
javaclassname(::Type{T}) where T <: AbstractVector = JavaCall.signature(T)
498498

499+
function _notnull_assert(ptr)
500+
isnull(ptr) && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
501+
end
502+
503+
function get_exception_string(jthrow)
504+
jthrowable = JNI.FindClass("java/lang/Throwable")
505+
_notnull_assert(jthrowable)
506+
507+
tostring_method = JNI.GetMethodID(jthrowable, "toString", "()Ljava/lang/String;")
508+
_notnull_assert(tostring_method)
509+
510+
res = JNI.CallObjectMethodA(jthrow, tostring_method, Int[])
511+
_notnull_assert(res)
512+
513+
return unsafe_string(JString(res))
514+
end
499515

500516
function geterror()
501517
isexception = JNI.ExceptionCheck()
502518

503519
if isexception == JNI_TRUE
504520
jthrow = JNI.ExceptionOccurred()
505-
isnull(jthrow) && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
521+
_notnull_assert(jthrow)
506522
try
507523
JNI.ExceptionDescribe() #Print java stackstrace to stdout
508-
jclass = JNI.FindClass("java/lang/Throwable")
509-
isnull(jclass) && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
510-
jmethodId=JNI.GetMethodID(jclass, "toString", "()Ljava/lang/String;")
511-
isnull(jmethodId) && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
512-
res = JNI.CallObjectMethodA(jthrow, jmethodId, Int[])
513-
isnull(res) && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
514-
msg = unsafe_string(JString(res))
524+
525+
msg = get_exception_string(jthrow)
515526
throw(JavaCallError(string("Error calling Java: ", msg)))
516527
finally
517528
JNI.ExceptionClear()

0 commit comments

Comments
 (0)