public static void Main()
Console.WriteLine("Without fix, 2nd catch shows a different line\n");
throwAndRethrowException(false);
Console.WriteLine("With fix, 2nd catch shows the same line\n");
throwAndRethrowException(true);
static void throwAndRethrowException(bool usePreserveStacktraceFix){
throw new Exception("Shalalala");
Console.WriteLine("1st catch:"+ex.StackTrace);
if(usePreserveStacktraceFix){
RethrowExceptionButPreserveStackTrace(ex);
Console.WriteLine("2nd catch:"+ex.StackTrace);;
private static void RethrowExceptionButPreserveStackTrace(Exception exception)
System.Reflection.MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
preserveStackTrace.Invoke(exception, null);