在处理应用程序级别的.NET异常 [英] Handle .net exceptions at application level

查看:150
本文介绍了在处理应用程序级别的.NET异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读一对夫妇的文章而回,我想说明一个问题,你可以在.NET应用程序(特别是VB.net),允许例外发生,然后将其处理在某些类型的应用程序-level异常处理程序,而不是一个try / catch块中。 我的谷歌富薄弱的时刻,所以我运气不好猜测了这个功能称为以便找到关于它的信息。

I was reading a couple of articles a while back which I think described a behaviour where you can, in a .net application (specifically VB.net), allow an exception to occur, and then handle it in some kind of application-level exception handler, as opposed to within a Try/Catch block. My google-fu is weak at the moment, so I'm not having much luck guessing what this feature is called in order to find information about it.

如果这听起来很耳熟任何你,你可以点我在正确的方向上究竟该功能被称为这样我就可以搜索到它?

If this rings a bell for any of you, can you point me in the right direction on what exactly the feature is called so that I can search for it?

样品code总是欢迎的,当然,不过这主要是我只是忘记该功能的名称,暂时无法搜索它的情况。

Sample code is always welcome, of course, but this is mostly just a case of me forgetting the name of the feature and being unable to search for it.

推荐答案

我相信你正在寻找AppDomain.UnhandledException事件。这将允许您处理任何异常就是未处理在您选择的AppDomain中。大多数应用程序只有一个单一的AppDomain所以这个code应该做的伎俩

I believe you are looking for the AppDomain.UnhandledException event. This will allow you to handle any exception that is unhandled in the AppDomain of your choice. Most applications only have a single AppDomain so this code should do the trick

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyHandler

然而,实际上试图在这里处理该事件是不是最好的主意。这将触发从任何部分code抛出的异常。这可能很容易成为一个例外是致命的,你依靠和处理这可能导致进一步的,在路上更糟糕的错误的一些组件。

However, actually attempting to handle the event here is not the best idea. This will fire for any exception thrown from any part of the code. This could very easily be an exception that is fatal to some component you depend on and handling it could lead to further, worse errors down the road.

我通常使用此事件纯属记录和错误报告的情况。

I usually use this event purely logging and error reporting scenarios.

这篇关于在处理应用程序级别的.NET异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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