Re-throw exception in task (TPL) loses stack trace

2019-04-11 04:35发布

问题:

I've code that rethrows an exception.

When I later read the exception from task.Exception, its stacktrace points to the location where I re-threw the exception (line n and not line m, as I expected).

Why is this so? bug in TPL or more likely something I have overlooked.

As I workaround I can wrap the exception as the inner-exception in a new exception.

internal class Program
{
    private static void Main(string[] args)
    {
        Task.Factory.StartNew(TaskMethod).ContinueWith(t => Console.WriteLine(t.Exception.InnerException));
        Console.Read();
    }

    private static void TaskMethod()
    {
        try
        {
line m:     throw new Exception("Todo");
        }
        catch (Exception)
        {
line n:     throw;
        }
    }
}

回答1:

Unfortunately, due to the way TPL stores exceptions until the task finished executing, the original stack trace is lost. Running your sample code in LINQPad shows that the exception was thrown at at System.Threading.Tasks.Task.Execute(), which is obviously not correct.

As a crude workaround, you could store the original stack trace (it being a simple string) on the Data property of the original exception, and you'll be able to access it then:

private static void TaskMethod()
{
    try
    {           
        throw new Exception("Todo");
    }
    catch (Exception ex)
    {
        ex.Data["OriginalStackTrace"] = ex.StackTrace;
        throw;
    }
}

Then you'll have the original stack trace stored in the OriginalStackTrace value of the Data dictionary:

It's not really what you want, but I hope it helps.