查找未处理的异常 - .NET

发布于 2024-12-06 08:24:21 字数 144 浏览 4 评论 0原文

我有一个相当大的 .NET 解决方案,但异常处理能力很差。原作者的本意是好的,捕获特定类型的异常而不是基本异常类,但是,这导致了我无法计算的地方抛出了某种类型的异常,但没有在任何地方捕获,并使软件变得不可靠。如何执行代码分析来确定解决方案中可以抛出异常类型但未被捕获的位置?

I have a fairly large .NET solution that has poor exception handling. The original authors meant well, catching specific types of exceptions rather than the base exception class, however, this has resulted in more than I can count places where exceptions of a type are thrown but not caught anywhere and makes the software unreliable. How can I perform a code analysis to identify where in the solution an exception type can be thrown but is not being caught?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶茶白久 2024-12-13 08:24:21

您可以添加一个全局异常处理程序来记录异常(包括堆栈跟踪),并为用户提供一个漂亮的错误对话框。您可以使用堆栈跟踪来识别代码中生成错误的位置,并更好地处理这些情况。尽可能多地记录。

如何做到这一点取决于平台(WebForms、MVC、WinForms、WPF,...?)

此外,对整个代码库进行深入的代码审查(重点是错误处理)可能会在许多错误表现为错误之前捕获它们给用户。

You can add a global exception handler that logs the Exception including stack trace, and puts up a nice error dialog for the user. You can use the stack trace to identify the places in the code that generates errors and put up better handling of those scenarios. Log as much as possible.

How you do that depends on the platform (WebForms, MVC, WinForms, WPF, ... ?)

Also, an in-depth code review of the entire code base with emphasis on error handling might catch many errors before they manifest themselves as errors to the user.

百合的盛世恋 2024-12-13 08:24:21

这就是我处理它们的方式。在我的 Program.csMain 方法中,我设置了事件处理。之后,您将所需的代码放入这些事件中。

Application.ThreadException += Form1_UIThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

编辑:这适用于非基于网络的应用程序。

This is how I handle them. In my Program.cs in the Main method, I set up event handling. After, you put in these events the code your want.

Application.ThreadException += Form1_UIThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

EDIT: This is for non-web based apps.

一向肩并 2024-12-13 08:24:21

任何未捕获的异常都不会得到处理。好消息是您可以订阅 AppDomain.CurrentDomain.UnhandledException 事件。

这会处理抛出但未被捕获的任何异常。

如何执行代码分析来确定解决方案中的哪些位置
异常类型可以抛出但未被捕获?

有像 StyleCop 这样的程序可以突出显示可能的未处理异常。

Any exception that is not caught is unhandled. The good news is you can subscribe to that AppDomain.CurrentDomain.UnhandledException event.

This handles any exception that is thrown that is not caught.

How can I perform a code analysis to identify where in the solution an
exception type can be thrown but is not being caught?

There are programs like StyleCop that will highlight possible unhandled exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文