简体   繁体   English

TPL打破未处理的异常

[英]TPL Break on unhandled exceptions

I am using async await as well as Task.Factory.StartNew in my application but one thing that i noticed changed is that visual studio is no more breaking when an unhandled exception occured 我在我的应用程序中使用async await以及Task.Factory.StartNew ,但我注意到的一件事是,当未处理的异常发生时,visual studio不再破坏

Here is what i mean by before using await 这是我before using await意思 在此输入图像描述

but after i turn a method into a Task and use await 但在我将一个方法转换为一个Task并使用await之后

在此输入图像描述

It is only captured in the output area in visual studio... 它仅在视觉工作室的输出区域中捕获...

BTW : It is very strange for me as i am new to .Net4.5 please excuse me if failed to illustrate what i need specifically but again what i want to know is 顺便说一下:对我来说很奇怪,因为我是.Net4.5的新手,请原谅我,如果没有说明我需要的具体内容,那么我想知道的是

  • How could i make visual studio break on the exceptions when using async await 当使用异步等待时,我如何使视觉工作室中断异常

When the debugger says "Exception was unhandled by user code", what it means is that an exception has propagated up to the framework. 当调试器说“Exception未被用户代码处理”时,这意味着异常已经传播到框架。 Since an async Task method places its exceptions on its returned Task , the exception does not propagate to the framework. 由于async Task方法将其异常放在其返回的Task ,因此异常不会传播到框架。 An exception like this is unobserved . 像这样的例外是未被观察到的

If you want the debugger to break when exceptions are thrown , then use Debugger -> Exceptions -> Check the "Thrown" box for CLR Exceptions. 如果希望调试器在抛出异常时中断,则使用Debugger - > Exceptions - >选中CLr Exceptions的“Thrown”框。

If you want to observe the exception, then change from TaskFactory.StartNew to Task.Run and call Wait on the returned Task . 如果要观察异常, 则从TaskFactory.StartNew更改为Task.Run并在返回的Task上调用Wait This will propagate the exception (wrapped in an AggregateException ) through Main and up to the framework. 这将通过Main传播异常(包装在AggregateException ),直到框架。

That's why void returning async methods are evil. 这就是为什么无效返回异步方法是邪恶的。

On the first case the exception is not caught and the debugger is alerting you of just that. 在第一种情况下,没有捕获异常,调试器正在提醒您这一点。

On the second case it will be on the resulting task. 在第二种情况下,它将在结果任务上。

If you handle the exceptions inside the method you'll see similar behavior. 如果您处理方法中的异常,您将看到类似的行为。

Task.Run is just a streamlined version of TaskFactory.StartNew: Task.Run vs Task.Factory.StartNew Task.Run只是TaskFactory.StartNew的简化版本: Task.Run vs Task.Factory.StartNew

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM