首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WinForms全局异常处理?

WinForms全局异常处理?
EN

Stack Overflow用户
提问于 2011-11-16 15:36:09
回答 7查看 36.7K关注 0票数 33

我已经实现了一个软件,它有一个DLL库,其中包含一组巨大的类,其中包括我的软件的所有方法。

现在,我希望能够处理一些全局错误,比如error #26,它是所有这些类上的一个与网络无关的错误,而不是去每个类并添加它。如何做到这一点?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-11-16 15:45:12

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

代码语言:javascript
复制
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);
}
票数 29
EN

Stack Overflow用户

发布于 2011-11-16 15:38:34

因为它是一个winforms应用程序,所以你可以将Application.Run(new MainForm());封装在一个try catch块中。

代码语言:javascript
复制
static void Main()
{
try {
 Application.Run(new MainForm());
} catch(SystemException)//just as an example 
{
 //log or handle the error here. 
}
}

我不知道这种解决方案会带来什么影响,但我只是告诉你你需要什么。

其他选项是订阅Application.ThreadException事件。

点击此处阅读更多信息:unhandledexceptions

还有AppDomain.UnhandledException,您应该阅读它们之间的区别here on MSDN

来自MSDN:

对于某些应用程序模型,如果未处理的异常发生在主应用程序线程中,则UnhandledException事件可能会被其他事件抢占。

在使用Windows窗体的应用程序中,主应用程序线程中未处理的异常会导致引发Application.ThreadException事件。如果处理了此事件,则默认行为是,尽管应用程序处于未知状态,但未处理的异常不会终止应用程序。在这种情况下,不会引发UnhandledException事件。可以使用应用程序配置文件更改此行为,或者在挂接ThreadException事件处理程序之前使用Application.SetUnhandledExceptionMode方法将模式更改为UnhandledExceptionMode.ThrowException。这只适用于主应用程序线程。对于在其他线程中引发的未处理异常,将引发UnhandledException事件。

票数 22
EN

Stack Overflow用户

发布于 2017-03-23 14:15:56

通过在C# Windows应用程序中引用Centralized Exception Handling,我找到了一个很好的解决方案:

代码语言:javascript
复制
static class Program
{
    [STAThread]
    static void Main()
    {
        // Add handler to handle the exception raised by main threads
        Application.ThreadException += 
        new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

        // Add handler to handle the exception raised by additional threads
        AppDomain.CurrentDomain.UnhandledException += 
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        // Stop the application and all the threads in suspended state.
        Environment.Exit(-1);            
    }

    static void Application_ThreadException
        (object sender, System.Threading.ThreadExceptionEventArgs e)
    {// All exceptions thrown by the main thread are handled over this method

        ShowExceptionDetails(e.Exception);
    }

    static void CurrentDomain_UnhandledException
        (object sender, UnhandledExceptionEventArgs e)
    {// All exceptions thrown by additional threads are handled in this method

        ShowExceptionDetails(e.ExceptionObject as Exception);

        // Suspend the current thread for now to stop the exception from throwing.
        Thread.CurrentThread.Suspend();
    }

    static void ShowExceptionDetails(Exception Ex)
    {
        // Do logging of exception details
        MessageBox.Show(Ex.Message, Ex.TargetSite.ToString(), 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}  

在上面的类中,我们将为两个事件附加一个事件处理程序。最好在main方法启动后立即附加这些事件。

Application.ThreadException -当主线程中抛出异常时,将引发此事件。如果我们添加一个事件处理程序,那么异常将通过该方法进行处理。

AppDomain.CurrentDomain.UnhandledException -当应用程序中使用的其他线程中抛出异常时,将引发此事件。这里最糟糕的情况是,一旦处理程序的执行结束,就会再次抛出异常,而应用程序会结束。这需要处理。在这里,我使用了一些代码来处理这种情况,并继续执行应用程序而不会中断。

我用来克服这种情况的逻辑只是挂起事件处理程序中的线程,以便应用程序继续正常工作。同样,挂起此线程时会出现问题。当主窗体关闭时,应用程序通常需要退出,但当线程处于挂起状态时,应用程序仍将保持运行状态。因此,要完全退出应用程序并停止进程,必须在结束main方法之前调用Environment.Exit(-1)。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8148156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档