Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private static unsafe uint RunFinalizers()
{
((delegate*<object, void>)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.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ private static unsafe uint DrainQueue()
// Call the finalizer on the current target object.
((delegate*<object, void>)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.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exception, bool>? s_handler;

internal static bool IsHandledByGlobalHandler(Exception ex)
{
return s_handler?.Invoke(ex) == true;
}

/// <summary>
/// Sets a handler for unhandled exceptions.
/// </summary>
/// <exception cref="ArgumentNullException">If handler is null</exception>
/// <exception cref="InvalidOperationException">If a handler is already set</exception>
public static void SetUnhandledExceptionHandler(UnhandledExceptionHandler handler)
public static void SetUnhandledExceptionHandler(Func<Exception, bool> handler)
{
ArgumentNullException.ThrowIfNull(handler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ private static void DispatchWorkItem(object workItem, Thread currentThread)
{
Unsafe.As<IThreadPoolWorkItem>(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.
}
Expand Down
3 changes: 1 addition & 2 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Exception,bool> handler) { }
}
public partial class FirstChanceExceptionEventArgs : System.EventArgs
{
Expand All @@ -13988,7 +13988,6 @@ public sealed partial class HandleProcessCorruptedStateExceptionsAttribute : Sys
{
public HandleProcessCorruptedStateExceptionsAttribute() { }
}
public delegate bool UnhandledExceptionHandler(System.Exception exception);
}
namespace System.Runtime.InteropServices
{
Expand Down