diff --git a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs index a093940e826a0f..15ec570949bb70 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs @@ -324,7 +324,7 @@ private static unsafe uint RunFinalizers() { ((delegate*)fptr)(target!); } - catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true) + catch (Exception ex) when (ExceptionHandling.IsHandledByGlobalHandler(ex)) { // the handler returned "true" means the exception is now "handled" and we should continue. } diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs index 7e9f62d0829da9..dce232600ff1ce 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs @@ -69,9 +69,7 @@ private static unsafe uint DrainQueue() // Call the finalizer on the current target object. ((delegate*)target.GetMethodTable()->FinalizerCode)(target); } - // We do not use "?." operator here like in other places. - // It would cause "Predefined type 'System.Nullable`1' is not defined" errors. - catch (Exception ex) when (ExceptionHandling.s_handler != null && ExceptionHandling.s_handler(ex)) + catch (Exception ex) when (ExceptionHandling.IsHandledByGlobalHandler(ex)) { // the handler returned "true" means the exception is now "handled" and we should continue. } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.cs index 0743463c15f133..dbe64d4381a3e4 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Thread.NativeAot.cs @@ -459,7 +459,7 @@ private static void StartThread(IntPtr parameter) startHelper.Run(); } - catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true) + catch (Exception ex) when (ExceptionHandling.IsHandledByGlobalHandler(ex)) { // the handler returned "true" means the exception is now "handled" and we should gracefully exit. } diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs b/src/coreclr/nativeaot/Test.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs index b9fb60a040fee7..2f341c130cbf3e 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs +++ b/src/coreclr/nativeaot/Test.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs @@ -5,10 +5,11 @@ namespace System.Runtime.ExceptionServices { - public delegate bool UnhandledExceptionHandler(System.Exception exception); - public static class ExceptionHandling { - internal static UnhandledExceptionHandler? s_handler; + internal static bool IsHandledByGlobalHandler(Exception ex) + { + return false; + } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs index 8921c18a91fd5e..7acd5b3f56b502 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/ExceptionServices/ExceptionHandling.cs @@ -5,18 +5,21 @@ namespace System.Runtime.ExceptionServices { - public delegate bool UnhandledExceptionHandler(System.Exception exception); - public static class ExceptionHandling { - internal static UnhandledExceptionHandler? s_handler; + private static Func? s_handler; + + internal static bool IsHandledByGlobalHandler(Exception ex) + { + return s_handler?.Invoke(ex) == true; + } /// /// Sets a handler for unhandled exceptions. /// /// If handler is null /// If a handler is already set - public static void SetUnhandledExceptionHandler(UnhandledExceptionHandler handler) + public static void SetUnhandledExceptionHandler(Func handler) { ArgumentNullException.ThrowIfNull(handler); diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index dd2c8798ad149d..b680b84c247619 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -86,7 +86,7 @@ private void RunWorker() parameterizedThreadStart(startArg); } } - catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true) + catch (Exception ex) when (ExceptionHandling.IsHandledByGlobalHandler(ex)) { // the handler returned "true" means the exception is now "handled" and we should gracefully exit. } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs index d7b9fd2a5c3d61..411bc6c6dce611 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs @@ -1220,7 +1220,7 @@ private static void DispatchWorkItem(object workItem, Thread currentThread) { Unsafe.As(workItem).Execute(); } - catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true) + catch (Exception ex) when (ExceptionHandling.IsHandledByGlobalHandler(ex)) { // the handler returned "true" means the exception is now "handled" and we should continue. } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 31c0c2a2f77783..0d79bc4c54e23b 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -13975,7 +13975,7 @@ internal ExceptionDispatchInfo() { } } public static partial class ExceptionHandling { - public static void SetUnhandledExceptionHandler(System.Runtime.ExceptionServices.UnhandledExceptionHandler handler) { } + public static void SetUnhandledExceptionHandler(System.Func handler) { } } public partial class FirstChanceExceptionEventArgs : System.EventArgs { @@ -13988,7 +13988,6 @@ public sealed partial class HandleProcessCorruptedStateExceptionsAttribute : Sys { public HandleProcessCorruptedStateExceptionsAttribute() { } } - public delegate bool UnhandledExceptionHandler(System.Exception exception); } namespace System.Runtime.InteropServices {