单元测试时如何处理其他线程中引发的异常?

编程入门 行业动态 更新时间:2024-10-24 20:14:56
本文介绍了单元测试时如何处理其他线程中引发的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用Visual Studio Team测试时测试中未处理的异常被捕获并在结果中报告。所以我很惊讶看到测试托管过程(VSTestHost.exe)崩溃,并显示系统崩溃对话框。

When testing with Visual Studio Team Test unhandled exceptions in tests are caught and reported in the results. So I was kind of surprised to see the test hosting process (VSTestHost.exe) crash and showing the system crash dialog.

进一步调查,这个崩溃是一个未处理的异常在另一个线程(更直接,它是一个异步套接字回调)。事实上这样的东西崩溃了托管过程:

Upon further investigation this crash was an unhandled exception raised in another thread (more directly, it was an async socket callback). And indeed something like this crashes the hosting process:

[TestMethod] void Test() { new Thread(() => { throw new Exception(); }).Start(); }

任何建议我应该做什么?

Any advices what I should do there?

  • 如果我只是用它,说任何代码分发/签入应该至少测试一次,所以这样的事情很可能会被捕获?
  • 我应该尝试安装全局异常处理程序,并检查其在每种拆卸方法中的状态吗?
  • 或者可能已经存在帮助这个的东西?
推荐答案

您可以使用全局异常处理程序捕获AppDomain中所有未捕获的异常:

You can use a Global Exception handler to catch all of the uncaught exception in the AppDomain:

AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new EventHandler(UnhandledExceptionHandler);

我认为它也适用于从其他线程抛出的异常

I think it would work for exceptions thrown from other threads as well

更多推荐

单元测试时如何处理其他线程中引发的异常?

本文发布于:2023-10-31 05:04:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   如何处理   单元测试   异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!