查找未处理的异常 - .NET
我有一个相当大的 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加一个全局异常处理程序来记录异常(包括堆栈跟踪),并为用户提供一个漂亮的错误对话框。您可以使用堆栈跟踪来识别代码中生成错误的位置,并更好地处理这些情况。尽可能多地记录。
如何做到这一点取决于平台(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.
这就是我处理它们的方式。在我的
Program.cs
的Main
方法中,我设置了事件处理。之后,您将所需的代码放入这些事件中。编辑:这适用于非基于网络的应用程序。
This is how I handle them. In my
Program.cs
in theMain
method, I set up event handling. After, you put in these events the code your want.EDIT: This is for non-web based apps.
任何未捕获的异常都不会得到处理。好消息是您可以订阅 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.
There are programs like StyleCop that will highlight possible unhandled exceptions.