diff --git a/src/hotspot/share/runtime/thread.cpp b/src/hotspot/share/runtime/thread.cpp index 27c46f92b80..b367688c0a3 100644 --- a/src/hotspot/share/runtime/thread.cpp +++ b/src/hotspot/share/runtime/thread.cpp @@ -3294,6 +3294,13 @@ void Threads::create_vm_init_libraries() { void JavaThread::invoke_shutdown_hooks() { HandleMark hm(this); + // We could get here with a pending exception, if so clear it now or + // it will cause MetaspaceShared::link_and_cleanup_shared_classes to + // fail for dynamic dump. + if (this->has_pending_exception()) { + this->clear_pending_exception(); + } + // Link all classes for dynamic CDS dumping before vm exit. // Same operation is being done in JVM_BeforeHalt for handling the // case where the application calls System.exit(). @@ -3301,11 +3308,6 @@ void JavaThread::invoke_shutdown_hooks() { MetaspaceShared::link_and_cleanup_shared_classes(this); } - // We could get here with a pending exception, if so clear it now. - if (this->has_pending_exception()) { - this->clear_pending_exception(); - } - EXCEPTION_MARK; Klass* shutdown_klass = SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),