static void ThrowExcThroughMethodsWithFinalizers2(string caller) { CreateSomeGarbage(); string s = caller + " + ThrowExcThroughMethodsWithFinalizers2"; CreateSomeGarbage(); try { throw new Exception("ThrowExcThroughMethodsWithFinalizers2"); } finally { Console.WriteLine("Executing finally in {0}", s); finallyCounter++; } }
public static int Main() { if (string.Empty.Length > 0) { // Just something to make sure we generate reflection metadata for the type new BringUpTest().ToString(); } int counter = 0; try { try { throw new Exception("My exception"); } catch (OutOfMemoryException) { Console.WriteLine("Unexpected exception caught"); return(Fail); } } catch (Exception e) { Console.WriteLine("Exception caught!"); if (e.Message != "My exception") { Console.WriteLine("Unexpected exception message!"); return(Fail); } string stackTrace = e.StackTrace; #if CODEGEN_WASM && !DEBUG //Wasm doesn't get useful names in release mode, e.g. it gets at wasm-function[10259]:0x4b5182 if (!stackTrace.Contains("wasm-function")) #else if (!stackTrace.Contains("BringUpTest.Main")) #endif { Console.WriteLine("Unexpected stack trace: " + stackTrace); return(Fail); } counter++; } try { g.myObjectField = new Object(); } catch (NullReferenceException) { Console.WriteLine("Null reference exception in write barrier caught!"); counter++; } try { try { g.myField++; } finally { counter++; } } catch (NullReferenceException) { Console.WriteLine("Null reference exception caught!"); counter++; } try { throw new Exception("Testing filter"); } catch (Exception e) when(e.Message == "Testing filter" && counter++ > 0) { Console.WriteLine("Exception caught via filter!"); if (e.Message != "Testing filter") { Console.WriteLine("Unexpected exception message!"); return(Fail); } counter++; } // test interaction of filters and finally clauses with GC try { ThrowExcThroughMethodsWithFinalizers1("Main"); } catch (Exception e) when(FilterWithGC() && counter++ > 0) { Console.WriteLine(e.Message); if (e.Message != "ThrowExcThroughMethodsWithFinalizers2") { Console.WriteLine("Unexpected exception message!"); return(Fail); } if (finallyCounter != 2) { Console.WriteLine("Finalizers didn't execute!"); return(Fail); } counter++; } try { try { throw new Exception("Hello"); } catch { counter++; throw; } } catch (Exception ex) { if (ex.Message != "Hello") { return(Fail); } counter++; } if (counter != 10) { Console.WriteLine("Unexpected counter value"); return(Fail); } return(Pass); }