-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
util.cc
46 lines (40 loc) · 1.13 KB
/
util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "util.h"
#include "vw/core/vw.h"
#include <exception>
// assume that the passed in object has a field "nativePointer" of type long
jlong get_native_pointer(JNIEnv* env, jobject obj)
{
jfieldID f = env->GetFieldID(env->GetObjectClass(obj), "nativePointer", "J");
return env->GetLongField(obj, f);
}
void throw_java_exception(JNIEnv* env, const char* name, const char* msg)
{
jclass jc = env->FindClass(name);
if (jc) env->ThrowNew(jc, msg);
}
void rethrow_cpp_exception_as_java_exception(JNIEnv* env)
{
try
{
throw;
}
catch (const std::bad_alloc& e)
{
throw_java_exception(env, "java/lang/OutOfMemoryError", e.what());
}
catch (const VW::vw_unrecognised_option_exception& e)
{
throw_java_exception(env, "java/lang/IllegalArgumentException", e.what());
}
catch (const std::exception& e)
{
throw_java_exception(env, "java/lang/Exception", e.what());
}
catch (...)
{
throw_java_exception(env, "java/lang/Error",
"Unidentified exception => "
"rethrow_cpp_exception_as_java_exception "
"may require some completion...");
}
}