TPL打破未处理的异常 [英] TPL Break on unhandled exceptions

查看:152
本文介绍了TPL打破未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 async 等待以及 Task.Factory.StartNew 在我的应用程序,但有一件事,我注意到改变是视觉工作室不再打破当一个未处理的异常发生

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

这是我的意思是在使用await

Here is what i mean by before using await

,但在将方法转换为任务并使用等待

but after i turn a method into a Task and use await

它只能在视觉工作室的输出区域捕获...

It is only captured in the output area in visual studio...

BTW:对我来说这是非常奇怪的,因为我是新来的.Net4.5请原谅我,如果失败为了说明我需要的具体而且我想知道的是

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


  • 使用$ b时,我如何使视觉工作室突破异常$ b 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. Since an async Task method places its exceptions on its returned Task, the exception does not propagate to the framework. An exception like this is unobserved.

如果您希望调试器在异常抛出时中断,那么请使用调试器 - >异常 - >检查CLR异常的抛出框。

If you want the debugger to break when exceptions are thrown, then use Debugger -> Exceptions -> Check the "Thrown" box for CLR Exceptions.

如果要观察异常,则将TaskFactory.StartNew 更改为 Task.Run ,并调用等待返回的任务。这将通过 Main 直到框架来传播异常(包裹在 AggregateException )。

If you want to observe the exception, then change from TaskFactory.StartNew to Task.Run and call Wait on the returned Task. This will propagate the exception (wrapped in an AggregateException) through Main and up to the framework.

这篇关于TPL打破未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆