diff --git a/src/tests/nativeaot/SmokeTests/Exceptions/Exceptions.cs b/src/tests/nativeaot/SmokeTests/Exceptions/Exceptions.cs index 3bc01acb80df9..3bf0f6de7ced6 100644 --- a/src/tests/nativeaot/SmokeTests/Exceptions/Exceptions.cs +++ b/src/tests/nativeaot/SmokeTests/Exceptions/Exceptions.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Runtime.ExceptionServices; using System.Text; public class BringUpTest @@ -153,6 +154,8 @@ public static int Main() return Fail; } + TestFirstChanceExceptionEvent(); + throw new Exception("UnhandledException"); return Fail; @@ -241,6 +244,43 @@ static int CatchGenericException(int a, int b) where T : Exception } } + static void TestFirstChanceExceptionEvent() + { + bool didInvokeHandler = false; + Exception exception = new Exception(); + EventHandler handler = (_, e) => + { + Console.WriteLine("Exception triggered FirstChanceException event handler"); + if (e.Exception != exception) + { + Console.WriteLine("Unexpected exception!"); + Environment.Exit(Fail); + } + + didInvokeHandler = true; + }; + Func 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);