Last active
October 15, 2025 18:38
-
-
Save MarkPflug/ae3440c57d2ec5cfcf140de099ecf924 to your computer and use it in GitHub Desktop.
TryCatchCrash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.CompilerServices; | |
| static class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| for (int i = 10; ; i += 10) | |
| { | |
| Console.WriteLine(i); | |
| try | |
| { | |
| // crashes at (net48/net10.0) | |
| // 120/190 | |
| //TestCrashTC(i); | |
| // 15630/23910 | |
| TestCrash(i); | |
| } | |
| catch { | |
| // throw Exception will be caught | |
| // but the ultimate StackOverflow will cause a process crash. | |
| } | |
| } | |
| } | |
| static void TestCrashTC(int depth) | |
| { | |
| if (depth == 0) | |
| { | |
| throw new Exception(); | |
| } | |
| try | |
| { | |
| TestCrashTC(depth - 1); | |
| } | |
| catch | |
| { | |
| throw; | |
| } | |
| } | |
| // disable tail-recursion | |
| [MethodImpl(MethodImplOptions.NoOptimization)] | |
| static void TestCrash(int depth) | |
| { | |
| if (depth == 0) | |
| { | |
| throw new Exception(); | |
| } | |
| TestCrash(depth - 1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment