Skip to content

Commit df789b1

Browse files
authored
Merge pull request #78 from ExpandingMan/javaerrors
some slightly better error handling
2 parents 6569d1a + f652f9c commit df789b1

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

src/JavaCall.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using Compat.Sys: iswindows, islinux, isunix, isapple
1313
import DataStructures: OrderedSet
1414

1515
if VERSION < v"0.7-"
16-
using Compat: @warn, @error
16+
using Compat: @warn
1717
import Base: isnull
1818
Base.finalizer(f::Function, o) = Base.finalizer(o, f)
1919
else

src/convert.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ function convert(::Type{JavaObject{T}}, obj::JavaObject{S}) where {T,S}
88
ptr === C_NULL && geterror()
99
return JavaObject{T}(ptr)
1010
end
11-
isnull(obj) && @error("Cannot convert NULL")
11+
isnull(obj) && throw(ArgumentError("Cannot convert NULL"))
1212
realClass = ccall(jnifunc.GetObjectClass, Ptr{Nothing}, (Ptr{JNIEnv}, Ptr{Nothing} ), penv, obj.ptr)
1313
if isConvertible(T, realClass) #dynamic cast
1414
ptr = ccall(jnifunc.NewLocalRef, Ptr{Nothing}, (Ptr{JNIEnv}, Ptr{Nothing}), penv, obj.ptr)
1515
ptr === C_NULL && geterror()
1616
return JavaObject{T}(ptr)
1717
end
18-
@error("Cannot cast java object from $S to $T")
18+
throw(JavaCallError("Cannot cast java object from $S to $T"))
1919
end
2020

2121
#Is java type convertible from S to T.

src/core.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function jnew(T::Symbol, argtypes::Tuple, args...)
127127
(Ptr{JNIEnv}, Ptr{Nothing}, Ptr{UInt8}, Ptr{UInt8}), penv, metaclass(T),
128128
String("<init>"), sig)
129129
if jmethodId == C_NULL
130-
@error("No constructor for $T with signature $sig")
130+
throw(JavaCallError("No constructor for $T with signature $sig"))
131131
end
132132
return _jcall(metaclass(T), jmethodId, jnifunc.NewObjectA, JavaObject{T}, argtypes, args...)
133133
end
@@ -217,7 +217,7 @@ for (x, y, z) in [ (:jboolean, :(jnifunc.CallBooleanMethodA), :(jnifunc.CallStat
217217
end
218218
@assert callmethod != C_NULL
219219
@assert jmethodId != C_NULL
220-
isnull(obj) && @error("Attempt to call method on Java NULL")
220+
isnull(obj) && throw(JavaCallError("Attempt to call method on Java NULL"))
221221
savedArgs, convertedArgs = convert_args(argtypes, args...)
222222
result = ccall(callmethod, $x , (Ptr{JNIEnv}, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), penv, obj.ptr, jmethodId, convertedArgs)
223223
result==C_NULL && geterror()
@@ -254,7 +254,7 @@ global const _jmc_cache = Dict{Symbol, JavaMetaClass}()
254254
function _metaclass(class::Symbol)
255255
jclass=javaclassname(class)
256256
jclassptr = ccall(jnifunc.FindClass, Ptr{Nothing}, (Ptr{JNIEnv}, Ptr{UInt8}), penv, jclass)
257-
jclassptr == C_NULL && @error("Class Not Found $jclass")
257+
jclassptr == C_NULL && throw(JavaCallError("Class Not Found $jclass"))
258258
return JavaMetaClass(class, jclassptr)
259259
end
260260

@@ -275,28 +275,28 @@ function geterror(allow=false)
275275

276276
if isexception == JNI_TRUE
277277
jthrow = ccall(jnifunc.ExceptionOccurred, Ptr{Nothing}, (Ptr{JNIEnv},), penv)
278-
jthrow==C_NULL && @error("Java Exception thrown, but no details could be retrieved from the JVM")
278+
jthrow==C_NULL && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
279279
ccall(jnifunc.ExceptionDescribe, Nothing, (Ptr{JNIEnv},), penv ) #Print java stackstrace to stdout
280280
ccall(jnifunc.ExceptionClear, Nothing, (Ptr{JNIEnv},), penv )
281281
jclass = ccall(jnifunc.FindClass, Ptr{Nothing}, (Ptr{JNIEnv},Ptr{UInt8}), penv,
282282
"java/lang/Throwable")
283-
jclass==C_NULL && @error("Java Exception thrown, but no details could be retrieved from the JVM")
283+
jclass==C_NULL && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
284284
jmethodId=ccall(jnifunc.GetMethodID, Ptr{Nothing},
285285
(Ptr{JNIEnv}, Ptr{Nothing}, Ptr{UInt8}, Ptr{UInt8}), penv, jclass, "toString",
286286
"()Ljava/lang/String;")
287-
jmethodId==C_NULL && @error("Java Exception thrown, but no details could be retrieved from the JVM")
287+
jmethodId==C_NULL && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
288288
res = ccall(jnifunc.CallObjectMethodA, Ptr{Nothing},
289289
(Ptr{JNIEnv}, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), penv, jthrow, jmethodId,
290290
C_NULL)
291-
res==C_NULL && error("Java Exception thrown, but no details could be retrieved from the JVM")
291+
res==C_NULL && throw(JavaCallError("Java Exception thrown, but no details could be retrieved from the JVM"))
292292
msg = unsafe_string(JString(res))
293293
ccall(jnifunc.DeleteLocalRef, Nothing, (Ptr{JNIEnv}, Ptr{Nothing}), penv, jthrow)
294-
@error(string("Error calling Java: ",msg))
294+
throw(JavaCallError(string("Error calling Java: ",msg)))
295295
else
296296
if allow==false
297297
return #No exception pending, legitimate NULL returned from Java
298298
else
299-
@error("Null from Java. Not known how")
299+
throw(JavaCallError("Null from Java. Not known how"))
300300
end
301301
end
302302
end

src/jnienv.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,6 @@ struct JavaVM
329329
JNIInvokeInterface_::Ptr{JNIInvokeInterface}
330330
end
331331

332-
332+
struct JavaCallError <: Exception
333+
msg::String
334+
end

src/jvm.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function findjvm()
104104
for path in libpaths
105105
push!(errorMsg,"\n $path")
106106
end
107-
error(reduce(*,errorMsg));
107+
throw(JavaCallError(reduce(*,errorMsg)))
108108
end
109109

110110

@@ -158,8 +158,8 @@ end
158158

159159
isloaded() = isdefined(JavaCall, :jnifunc) && isdefined(JavaCall, :penv) && penv != C_NULL
160160

161-
assertloaded() = isloaded() ? nothing : @error("JVM not initialised. Please run init()")
162-
assertnotloaded() = isloaded() ? @error("JVM already initialised") : nothing
161+
assertloaded() = isloaded() ? nothing : throw(JavaCallError("JVM not initialised. Please run init()"))
162+
assertnotloaded() = isloaded() ? throw(JavaCallError("JVM already initialised")) : nothing
163163

164164
# Pointer to pointer to pointer to pointer alert! Hurrah for unsafe load
165165
function init(opts)
@@ -171,7 +171,7 @@ function init(opts)
171171
convert(Ptr{JavaVMOption}, pointer(opt)), JNI_TRUE)
172172
res = ccall(create, Cint, (Ptr{Ptr{JavaVM}}, Ptr{Ptr{JNIEnv}}, Ptr{JavaVMInitArgs}), ppjvm, ppenv,
173173
Ref(vm_args))
174-
res < 0 && @error("Unable to initialise Java VM: $(res)")
174+
res < 0 && throw(JavaCallError("Unable to initialise Java VM: $(res)"))
175175
global penv = ppenv[1]
176176
global pjvm = ppjvm[1]
177177
jnienv = unsafe_load(penv)
@@ -183,9 +183,9 @@ end
183183

184184
function destroy()
185185
if (!isdefined(JavaCall, :penv) || penv == C_NULL)
186-
@error("Called destroy without initialising Java VM")
186+
throw(JavaCallError("Called destroy without initialising Java VM"))
187187
end
188188
res = ccall(jvmfunc.DestroyJavaVM, Cint, (Ptr{Nothing},), pjvm)
189-
res < 0 && @error("Unable to destroy Java VM")
189+
res < 0 && throw(JavaCallError("Unable to destroy Java VM"))
190190
global penv=C_NULL; global pjvm=C_NULL;
191191
end

0 commit comments

Comments
 (0)