小编典典

WinForms全局异常处理?

c#

我已经实现了具有DLL库的软件,该DLL库包含大量的类,其中包括我的软件的所有方法。

现在,我希望能够处理一些全局错误,例如错误#26,这是所有这些类上的与网络无关的错误,而不是去每个类并将其添加。怎么做 ?


阅读 398

收藏
2020-05-19

共1个答案

小编典典

如果#26是异常,则可以使用AppDomain.CurrentDomain.UnhandledException事件。如果这只是一个返回值,那么我看不到有任何全局处理该值的机会。

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);
}
2020-05-19