summaryrefslogtreecommitdiffstats
path: root/java/jni/helper.cxx
diff options
context:
space:
mode:
authorPetre Eftime <[email protected]>2016-05-10 16:37:33 +0300
committerPetre Eftime <[email protected]>2016-05-10 16:37:33 +0300
commit44ea9ff1da0e10a7e0e8c14167f0fc7e8937bd3a (patch)
tree5b57dcc022a580241f81cfb7f75e896af50a5fb8 /java/jni/helper.cxx
parentc07b823b88c35b2e93ca2588afa119c31faf17fe (diff)
java: throw exceptions from JNI to Javav0.4.2
Signed-off-by: Petre Eftime <[email protected]>
Diffstat (limited to 'java/jni/helper.cxx')
-rw-r--r--java/jni/helper.cxx29
1 files changed, 26 insertions, 3 deletions
diff --git a/java/jni/helper.cxx b/java/jni/helper.cxx
index c701b931..f23691bc 100644
--- a/java/jni/helper.cxx
+++ b/java/jni/helper.cxx
@@ -120,10 +120,12 @@ bool from_jboolean_to_bool(jboolean val)
std::string from_jstring_to_string(JNIEnv *env, jstring str)
{
jboolean is_copy = JNI_TRUE;
+ if (!str) {
+ throw std::invalid_argument("String should not be null");
+ }
const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy);
- if (!str_chars)
- {
- throw std::runtime_error("GetStringUTFChars returned NULL\n");
+ if (!str_chars) {
+ throw std::bad_alloc();
}
const std::string string_to_write = std::string(str_chars);
@@ -196,3 +198,24 @@ jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add)
return result;
}
+
+void raise_java_exception(JNIEnv *env, std::exception &e)
+{
+ env->ThrowNew(env->FindClass("java/lang/Error"), e.what());
+}
+
+void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e)
+{
+ env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what());
+}
+
+void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e)
+{
+ env->ThrowNew(env->FindClass("java/lang/OutOfMemoryException"), e.what());
+}
+
+void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e)
+{
+ env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what());
+}
+