Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for the first chance exception event to NativeAOT smoke tests #86560

Merged
Merged
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
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