diff --git a/src/hotspot/share/runtime/sharedRuntime.cpp b/src/hotspot/share/runtime/sharedRuntime.cpp index e277e1fb569..3a7c60cf83e 100644 --- a/src/hotspot/share/runtime/sharedRuntime.cpp +++ b/src/hotspot/share/runtime/sharedRuntime.cpp @@ -809,6 +809,8 @@ address SharedRuntime::compute_compiled_exc_handler(nmethod* nm, address ret_pc, // determine handler bci, if any EXCEPTION_MARK; + Handle orig_exception(THREAD, exception()); + int handler_bci = -1; int scope_depth = 0; if (!force_unwind) { @@ -830,7 +832,7 @@ address SharedRuntime::compute_compiled_exc_handler(nmethod* nm, address ret_pc, // thrown (bugs 4307310 and 4546590). Set "exception" reference // argument to ensure that the correct exception is thrown (4870175). recursive_exception_occurred = true; - exception = Handle(THREAD, PENDING_EXCEPTION); + exception.replace(PENDING_EXCEPTION); CLEAR_PENDING_EXCEPTION; if (handler_bci >= 0) { bci = handler_bci; @@ -859,8 +861,10 @@ address SharedRuntime::compute_compiled_exc_handler(nmethod* nm, address ret_pc, // If the compiler did not anticipate a recursive exception, resulting in an exception // thrown from the catch bci, then the compiled exception handler might be missing. - // This is rare. Just deoptimize and let the interpreter handle it. + // This is rare. Just deoptimize and let the interpreter rethrow the original + // exception at the original bci. if (t == nullptr && recursive_exception_occurred) { + exception.replace(orig_exception()); // restore original exception bool make_not_entrant = false; return Deoptimization::deoptimize_for_missing_exception_handler(nm, make_not_entrant); } diff --git a/test/hotspot/jtreg/compiler/exceptions/IllegalAccessInCatch.jasm b/test/hotspot/jtreg/compiler/exceptions/IllegalAccessInCatch.jasm index beeffa69a97..78be0bbb177 100644 --- a/test/hotspot/jtreg/compiler/exceptions/IllegalAccessInCatch.jasm +++ b/test/hotspot/jtreg/compiler/exceptions/IllegalAccessInCatch.jasm @@ -46,6 +46,7 @@ super class IllegalAccessInCatch idiv; endtry t0; ireturn; + catch t0 java/lang/IllegalAccessError; catch t0 jdk/internal/agent/AgentConfigurationError; // loadable but not accessible from unnamed module stack_frame_type full; stack_map class java/lang/Throwable; diff --git a/test/hotspot/jtreg/compiler/exceptions/TestAccessErrorInCatch.java b/test/hotspot/jtreg/compiler/exceptions/TestAccessErrorInCatch.java index 44cfd60cf38..fa81fa93f11 100644 --- a/test/hotspot/jtreg/compiler/exceptions/TestAccessErrorInCatch.java +++ b/test/hotspot/jtreg/compiler/exceptions/TestAccessErrorInCatch.java @@ -64,6 +64,11 @@ public class TestAccessErrorInCatch { } private static int invoke(MethodHandle mh) throws Throwable { - return (int) mh.invokeExact(); + int expected = 1; + int ret = (int) mh.invokeExact(); + if (ret != expected) { + throw new RuntimeException("Returned " + ret + " but expected " + expected); + } + return ret; } }