问题描述
正如所指出的here和,例外异步任务存在的在技术上没有未处理的。
As indicated here and here, exceptions occuring in an async Task are technically not unhandled.
这与MVC工作时特别讨厌。它实际上我们花了一段时间才能弄清楚它为什么发生的越来越多的例外没有被抓住了,我们一直在逐步引入的Web API调用我们的aplication在过去的几个星期。
This is particularly nasty when working with MVC. It actually took us a while to figure out why it was occurring more and more that exceptions weren't being caught, we had been gradually introducing Web API calls to our aplication the past few weeks.
public async Task<ActionResult> Foo()
{
// ...
}
建议的解决方法是让所有的异常,而不是只未处理的异常VS休息。它的工作原理,与恼人的副作用,它确实对所有异常中断:)
The suggested workaround is to make VS break on all exceptions instead of only unhandled exceptions. It works, with the annoying 'side-effect' that it does indeed break on all exceptions :)
有,不涉及打破所有异常另一个解决方法吗?它可以具体到MVC的,但不必须被(意味着如果它是这种情况,为MVC工作的一般溶液)。
Is there another workaround that doesn't involve breaking on all exceptions? It can be specific to MVC but doesn't have to be (meaning if it's a general solution that happens to work for MVC).
推荐答案
借助对在异步方法例外一些指导和实例,并指出:完成任务等待着,是因为在返回的任务等待的方法未处理的异常的应用可能处于故障状态。任务抛出一个异常。
The try-catch reference in MSDN has some guidance and examples on exceptions in async methods, and states: "The completed task to which await is applied might be in a faulted state because of an unhandled exception in the method that returns the task. Awaiting the task throws an exception."
有关该网页上给出的例子,它指出:下面的例子说明了异常处理异步方法要赶上一个异步任务抛出一个异常,放置在的await 前pression。在尝试块,并捕获异常在捉块,取消对在本例中抛出新的异常
行表现出异常处理。该任务的 IsFaulted 属性设置为真,任务的 Exception.InnerException 属性设置为异常,异常被捕获在捉块。
For the example given on that page, it states: "The following example illustrates exception handling for async methods. To catch an exception that an async task throws, place the await expression in a try block, and catch the exception in a catch block. Uncomment the throw new Exception
line in the example to demonstrate exception handling. The task's IsFaulted property is set to True, the task's Exception.InnerException property is set to the exception, and the exception is caught in the catch block."
下面是给有例子中的副本:
Here's the copy from the example given there:
public async Task DoSomethingAsync()
{
Task<string> theTask = DelayAsync();
try
{
string result = await theTask;
Debug.WriteLine("Result: " + result);
}
catch (Exception ex)
{
Debug.WriteLine("Exception Message: " + ex.Message);
}
Debug.WriteLine("Task IsCanceled: " + theTask.IsCanceled);
Debug.WriteLine("Task IsFaulted: " + theTask.IsFaulted);
if (theTask.Exception != null)
{
Debug.WriteLine("Task Exception Message: "
+ theTask.Exception.Message);
Debug.WriteLine("Task Inner Exception Message: "
+ theTask.Exception.InnerException.Message);
}
}
private async Task<string> DelayAsync()
{
await Task.Delay(100);
// Uncomment each of the following lines to
// demonstrate exception handling.
//throw new OperationCanceledException("canceled");
//throw new Exception("Something happened.");
return "Done";
}
// Output when no exception is thrown in the awaited method:
// Result: Done
// Task IsCanceled: False
// Task IsFaulted: False
// Output when an Exception is thrown in the awaited method:
// Exception Message: Something happened.
// Task IsCanceled: False
// Task IsFaulted: True
// Task Exception Message: One or more errors occurred.
// Task Inner Exception Message: Something happened.
// Output when a OperationCanceledException or TaskCanceledException
// is thrown in the awaited method:
// Exception Message: canceled
// Task IsCanceled: True
// Task IsFaulted: False
这篇关于如何使在一个异步任务异常VS破,不破对所有异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!