Skip to content

Commit

Permalink
Fix exception describe
Browse files Browse the repository at this point in the history
  • Loading branch information
chippmann committed May 25, 2024
1 parent ccef2c5 commit 295bae9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/jni/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace jni {
String Env::exception_describe() {
#ifdef DEBUG_ENABLED
if (jthrowable e = env->ExceptionOccurred()) {
env->ExceptionClear(); // needs to be cleared now as otherwise it spams errors. We already have a ref to the exception.
jclass string_writer_class {env->FindClass("java/io/StringWriter")};
jmethodID string_writer_constructor {env->GetMethodID(string_writer_class, "<init>", "()V")};
jobject string_writer {env->NewObject(string_writer_class, string_writer_constructor)};
Expand All @@ -53,9 +54,19 @@ namespace jni {
jmethodID print_stack_trace_method {env->GetMethodID(throwable_class, "printStackTrace", "(Ljava/io/PrintWriter;)V")};
env->CallVoidMethod(e, print_stack_trace_method, print_writer);

if (exception_check()) {
exception_clear();
return {};
}

jmethodID to_string_method {env->GetMethodID(string_writer_class, "toString", "()Ljava/lang/String;")};
auto j_stack_trace_string {(jstring) env->CallObjectMethod(string_writer, to_string_method)};

if (exception_check()) {
exception_clear();
return {};
}

const char* c_stack_trace {env->GetStringUTFChars(j_stack_trace_string, nullptr)};
String stack_trace {c_stack_trace};
env->ReleaseStringUTFChars(j_stack_trace_string, c_stack_trace);
Expand All @@ -69,7 +80,10 @@ namespace jni {
}

void Env::exception_clear() {
// no op if DEBUG_ENABLED. Then the exception is cleared in exception_describe
#ifndef DEBUG_ENABLED
env->ExceptionClear();
#endif
}

void Env::check_exceptions() {
Expand Down Expand Up @@ -114,4 +128,4 @@ namespace jni {
env->GetJavaVM(&jvm);
return jvm;
}
}// namespace jni
}// namespace jni

0 comments on commit 295bae9

Please sign in to comment.