Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Winforms中未处理的异常处理_C#_Winforms_Exception Handling - Fatal编程技术网

C# Winforms中未处理的异常处理

C# Winforms中未处理的异常处理,c#,winforms,exception-handling,C#,Winforms,Exception Handling,在Form1.cs中,我有以下内容 [STAThread] static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException +=

在Form1.cs中,我有以下内容

[STAThread]
    static void Main()
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException +=
                new ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.Run(new Form1());                
        }
        catch (Exception e)
        {                

        }
    }
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
   //Code
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
   //Code
}
但异常仍显示为未处理。有人能建议如何通过上面声明的事件处理程序捕获异常吗


谢谢。

线程异常事件仅适用于UI线程。不会捕获在其他线程中发生的所有异常

您可以使用
AppDomain.Current.UnhandledException
事件捕获这些异常。但是,您无法使用该事件防止应用程序崩溃


因此,您必须始终确保在所有其他线程中使用
try
/
catch

我使用的是AppDomain.Current.UnhandledException,但它仍然不起作用。我做错了吗?谢谢。
但异常仍显示为未处理
。您的
未处理异常
事件无法阻止应用程序崩溃。
private void backgroundWorkerLogin_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
     throw new Exception("Test");
}