Skip to content

Commit

Permalink
Add a test for the first chance exception event (#86560)
Browse files Browse the repository at this point in the history
  • Loading branch information
SingleAccretion authored May 22, 2023
1 parent 78e42f7 commit 8163d16
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/tests/nativeaot/SmokeTests/Exceptions/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using System.Text;

public class BringUpTest
Expand Down Expand Up @@ -153,6 +154,8 @@ public static int Main()
return Fail;
}

TestFirstChanceExceptionEvent();

throw new Exception("UnhandledException");

return Fail;
Expand Down Expand Up @@ -241,6 +244,43 @@ static int CatchGenericException<T>(int a, int b) where T : Exception
}
}

static void TestFirstChanceExceptionEvent()
{
bool didInvokeHandler = false;
Exception exception = new Exception();
EventHandler<FirstChanceExceptionEventArgs> handler = (_, e) =>
{
Console.WriteLine("Exception triggered FirstChanceException event handler");
if (e.Exception != exception)
{
Console.WriteLine("Unexpected exception!");
Environment.Exit(Fail);
}

didInvokeHandler = true;
};
Func<Exception, bool> check = e =>
{
if (!didInvokeHandler)
{
Console.WriteLine("Did not invoke FirstChanceException event handler!");
Environment.Exit(Fail);
}

return e == exception;
};

AppDomain.CurrentDomain.FirstChanceException += handler;
try
{
throw exception;
}
catch (Exception e) when (check(e))
{
}
AppDomain.CurrentDomain.FirstChanceException -= handler;
}

static bool FilterWithStackTrace(Exception e)
{
var stackTrace = new StackTrace(0, true);
Expand Down

0 comments on commit 8163d16

Please sign in to comment.