Skip to content

Instantly share code, notes, and snippets.

@ssa3512
Last active January 8, 2020 19:37
Show Gist options
  • Save ssa3512/c4ac0a1e3cff144ea78684d6ae478a92 to your computer and use it in GitHub Desktop.
Save ssa3512/c4ac0a1e3cff144ea78684d6ae478a92 to your computer and use it in GitHub Desktop.
.NET stack preservation
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
try
{
for (int i = 0; i < 100; i++)
{
if (i == 99)
{
throw new ApplicationException("test");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace); // Writes stack pointing to line 17
Console.WriteLine();
throw;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace); // Writes stack pointing to line 25, not helpful
}
Console.ReadLine();
}
}
}
// Output
// at ConsoleApp1.Program.Main(String[] args) in C:\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 17
// at ConsoleApp1.Program.Main(String[] args) in C:\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 25
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
try
{
BadWork();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace); // Writes stack with line 35
Console.WriteLine();
throw;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace); // Writes stack with line 35
}
Console.ReadLine();
}
static void BadWork()
{
for (int i = 0; i < 100; i++)
{
if (i == 99)
{
throw new ApplicationException("test");
}
}
}
}
}
// Output
// at ConsoleApp1.Program.BadWork() in C:\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 35
// at ConsoleApp1.Program.Main(String[] args) in C:\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 13
//
// at ConsoleApp1.Program.BadWork() in C:\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 35
// at ConsoleApp1.Program.Main(String[] args) in C:\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment