WinForms 全局异常处理? [英] WinForms Global Exception Handling?
问题描述
我已经实现了一个软件,它有一个 DLL 库,其中包含大量的类,其中包括我的软件的所有方法.
I have implemented a software which have a DLL library which contains a huge set of classes which includes all the methods for my software.
现在我希望能够处理一些全局错误,比如错误 #26,这是所有这些类上的无网络相关错误,而不是去每个类并添加它.如何做到这一点?
Now i want to be able to handle some global errors like error #26 which is a no Network Related Error on all these classes instead of going to each class and add it. How to do that ?
推荐答案
如果 #26
是一个异常,那么你可以使用 AppDomain.CurrentDomain.UnhandledException
事件.如果它只是一个返回值,那么我看不到任何机会在全局范围内处理它.
If #26
is an exception then you can use AppDomain.CurrentDomain.UnhandledException
event. If it's just a return value, then I don't see any chance to handle that globally.
public static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
// start main thread here
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}
这篇关于WinForms 全局异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!