我怎样才能制作出能够捕获所有“未处理”的东西? WinForms 应用程序中的异常?
到目前为止,我只是在程序的 Program.cs
入口点中的 Application.Run
周围放置了一个 try/catch 块。这在调试模式下可以很好地捕获所有异常,但是当我在没有调试模式的情况下运行程序时,异常不再得到处理。我收到未处理的异常框。
我不希望这种事发生。我希望在非调试模式下运行时捕获所有异常。该程序有多个线程,并且最好由同一个处理程序捕获它们的所有异常;我想在数据库中记录异常。有人对如何做到这一点有任何建议吗?
Up until now, I just put a try/catch block around the Application.Run
in the Program.cs
entry point to the program. This catches all exceptions well enough in Debug mode, but when I run the program without the debug mode, exceptions don't get handled anymore. I get the unhandled exception box.
I don't want this to happen. I want all exceptions to be caught when running in non-debug mode. The program has multiple threads and preferably all exceptions from them get caught by the same handler; I want to log exceptions in the DB. Does anyone have any advice in how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看ThreadException 文档中的示例< /a>:
您可能还希望在调试时不捕获异常,因为这样可以更轻松地进行调试。这有点像黑客,但为此,您可以将上面的代码包装起来,
以防止在调试时捕获异常。
编辑:检查调试器内运行的应用程序的另一种方法,感觉比检查文件名更干净。
(请参阅moltenform、Kiquenet 和Doug 的评论)
这避免了使用与
vshost.exe
不同的调试器的问题。Take a look at the example from the ThreadException documentation:
You might also want to not catch exceptions when debugging, as this makes it easier to debug. It is somewhat of a hack, but for that you can wrap the above code around with
To prevent catching the exceptions when debugging.
EDIT: An alternate way to check for your application running inside a debugger that feels cleaner than checking a filename.
(see comments by moltenform, Kiquenet and Doug)
This avoids the problem of using a different debugger than
vshost.exe
.在 NET 4 中,默认情况下不再捕获某些异常;是指示可执行文件的(可能是致命的)损坏状态的异常,例如 AccessViolationException。
尝试在 main 方法前面使用 [HandleProcessCorruptedStateExceptions] 标记,例如
In NET 4, certain exceptions are no longer caught by default; these tend to be exceptions that indicate a (possibly fatal) corrupted state of the executable, such as an AccessViolationException.
Try using the [HandleProcessCorruptedStateExceptions] tag in front of your main method, e.g.
可以在 http://www.csharp-examples.net/having 找到一个很好的示例-未处理的异常/
基本上,将您的 main 更改为:
A nice example can be found at http://www.csharp-examples.net/catching-unhandled-exceptions/
Basically, change your main to:
您可以使用 NBug 库来实现。通过这样的最少设置:
您可以开始收集有关应用程序中所有未处理错误的信息,即使它已部署到客户端。如果您不想使用第三方库,您应该附加以下事件:
You can use NBug library for that. With minimal setup like this:
You can start collecting information on all unhandled bugs in your application, even when it's deployed to the clients. If you don't want to use a 3rd party library, you should attach to below events: