diff --git a/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c b/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c index ad62c918051e..9f20147f7f55 100644 --- a/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c +++ b/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include @@ -311,10 +311,15 @@ JNIEXPORT jobject JNICALL JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject acti return (*env)->CallObjectMethod(env, action, run); } } + + /* Some error occurred - clear pending exception and try to report the error. */ + (*env)->ExceptionClear(env); + jclass errorClass = (*env)->FindClass(env, "java/lang/InternalError"); if (errorClass != NULL && !(*env)->ExceptionCheck(env)) { (*env)->ThrowNew(env, errorClass, "Could not invoke PrivilegedAction"); } else { + (*env)->ExceptionClear(env); (*env)->FatalError(env, "PrivilegedAction could not be invoked and the error could not be reported"); } return NULL;