If an uncommon trap happens because a klass is not loaded yet, we will try to load it by calling `Deoptimization::load_class_by_index`. If there is an exception thrown while trying to load the class we clear it, relying on the fact that it will be thrown again once we are back in the interpreter to re-execute the bytecode. The exception is cleared using CLEAR_PENDING_NONASYNC_EXCEPTION, which is implemented as:
if ((_pending_exception->klass() != vmClasses::InternalError_klass() ||
java_lang_InternalError::during_unsafe_access(_pending_exception) != JNI_TRUE)) {
clear_pending_exception();
}
This means we only consider as an async exception an InternalError in accesses through the Unsafe class, which is incorrect. For JVMTI StopThread, any exception could be sent as an async exception. Also for ScopedMemoryAccess we use asynchronous exceptions of type ScopedAccessError.