changeset 27:4a750ca9461e

logging message associated with an exception
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Jun 2024 19:14:51 +0200
parents a4326b544c51
children 43923934e934
files Plugin/JavaEnvironment.cpp
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/JavaEnvironment.cpp	Sat Apr 06 17:24:15 2024 +0200
+++ b/Plugin/JavaEnvironment.cpp	Tue Jun 11 19:14:51 2024 +0200
@@ -23,6 +23,7 @@
 
 
 #include "JavaEnvironment.h"
+#include "JavaString.h"
 
 #include <cassert>
 #include <stdexcept>
@@ -98,7 +99,20 @@
 {
   if (env_->ExceptionCheck() == JNI_TRUE)
   {
+    jthrowable e = env_->ExceptionOccurred();
     env_->ExceptionClear();
+
+    jclass clazz = env_->GetObjectClass(e);
+    if (clazz != NULL)
+    {
+      jmethodID getMessage = env_->GetMethodID(clazz, "getMessage", "()Ljava/lang/String;");
+      if (getMessage != NULL)
+      {
+        JavaString message(env_, (jstring) env_->CallObjectMethod(e, getMessage));
+        throw std::runtime_error("An exception has occurred in Java: " + std::string(message.GetValue()));
+      }
+    }
+
     throw std::runtime_error("An exception has occurred in Java");
   }
 }