【发布时间】:2016-01-09 15:39:59
【问题描述】:
使用 Visual Studio 2015,以 FW 4 为目标(在 FW 4 下测试不可观察的异常):
我期待这个代码:
static void Main(string[] args)
{
try
{
Task.Factory.StartNew(() => Console.WriteLine(1))
.ContinueWith(t => Thread.Sleep(1000))
.ContinueWith(t => Console.WriteLine(2))
.ContinueWith(t => Thread.Sleep(1000))
.ContinueWith(t => { throw new Exception("aaaa"); })
.ContinueWith(t => Console.WriteLine(3));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
GC.Collect();
GC.Collect();
Console.ReadLine();
}
向我展示异常。
我知道我可以通过 T.Wait() 或在最后一个任务中使用 t.Exception 看到它 - 但为什么我在这里没有看到异常?
我知道异常处理机制在 4.5 中改变了,为了获得旧的机制,我应该添加:
<ThrowUnobservedTaskExceptions enabled="true"/>
我做了什么:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>
但结果还是:
问题:
为什么我没有看到异常?
值得一提的是,我确实在调试模式下看到了异常:
【问题讨论】:
-
你试过
throw new BackoffException("aaaaa"); -
它也可能是一个内部异常。如果是这样,您需要检查内部异常,称为聚合异常...
-
您可以检查的另一件事。在不调试或禁用“仅我的代码”的情况下运行(工具 -> 选项 -> 调试 -> 常规)...
-
@Codexer 已经禁用了。
标签: c# continuewith