Skip to content

Instantly share code, notes, and snippets.

@MarkPflug
Last active October 15, 2025 18:38
Show Gist options
  • Select an option

  • Save MarkPflug/ae3440c57d2ec5cfcf140de099ecf924 to your computer and use it in GitHub Desktop.

Select an option

Save MarkPflug/ae3440c57d2ec5cfcf140de099ecf924 to your computer and use it in GitHub Desktop.
TryCatchCrash
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