这个例子“失败”:
static async void Main(string[] args)
{
try
{
await TaskEx.Run(() => { throw new Exception("failure"); });
}
catch (Exception)
{
throw new Exception("success");
}
}
也就是说,带有文本“failure”的异常会往上传递。
然后我尝试了这个解决方法:
static async void Main(string[] args)
{
try
{
await SafeRun(() => { throw new Exception("failure"); });
}
catch (Exception)
{
throw new Exception("success");
}
}
static async Task SafeRun(Action action)
{
var ex = default(Exception);
await TaskEx.Run(() =>
{
try
{
action();
}
catch (Exception _)
{
ex = _;
}
});
if (ex != default(Exception))
throw ex;
}
那也没有帮助。
我猜我的 Async CTP 刷新安装可能出了问题。
这段代码应该按照我的期望工作("success" 上升,而不是 "failure"),还是说本来就不应该这样工作?如果不行,你会怎么处理?
async
入口点而导致的行为。 - Arne Claassen