c# - 我自己的异常处理程序仍然抛出异常并使应用程序崩溃

标签 c# winforms exception handler

Winforms 应用程序。 这是主要的:

  static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // Add handler for UI thread exceptions
        Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);

        // Force all WinForms errors to go through handler
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // This handler is for catching non-UI thread exceptions
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

        Application.Run(new Form1());
    }

    private static void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
    {
            Exception ex = (Exception)e.ExceptionObject;
            MessageBox.Show("Unhadled domain exception:\n\n" + ex.Message);

            Application.Exit();


        // It should terminate our main thread so Application.Exit() is unnecessary here
    }

    private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
    {
         MessageBox.Show("Unhandled exception catched.\n Application is going to close now.");


        // Here we can decide if we want to end our application or do something else
        Application.Exit();
    }
}

这是我故意生成异常的地方

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int zero = 0;
            //int number = 1 / zero;

            throw new System.IO.FileNotFoundException();
        }
    }

堆栈跟踪:

    System.IO.FileNotFoundException was unhandled
  HResult=-2147024894
  Message=Unable to find the specified file.
  Source=NewPostSharpSolution
  StackTrace:
       at NewPostSharpSolution.Form1..ctor() in C:\Users\Joao\Documents\Visual Studio 2015\Projects\NewPostSharpSolution\NewPostSharpSolution\Form1.cs:line 21
       at NewPostSharpSolution.Program.Main() in C:\Users\Joao\Documents\Visual Studio 2015\Projects\NewPostSharpSolution\NewPostSharpSolution\Program.cs:line 30
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

处理程序显示异常的消息框,但仍然破坏应用程序...

我有什么遗漏的吗?我认为实现这个处理程序可以让我决定如何处理异常?

最佳答案

来自 MSDN AppDomain.UnhandledException Event :

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.

这意味着 - 你无法处理异常。您可以记录有关异常的信息,显示一些消息等。但您无法阻止应用程序终止。

关于c# - 我自己的异常处理程序仍然抛出异常并使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41876258/

相关文章:

c# - Windows 后台服务可根据需要按名称终止进程

C#/WPF : Dependency Property is not updating the bound Property?

c# - 使用本地数据库为 winForms 创建安装程序

C# 应用程序范围的鼠标左键单击事件

postgresql - postgresql嵌套异常可能吗?

Python:在不引发异常的情况下检查字符串是否为 JSON?

C# 锁和异步方法

c# - 更改 ComboBox 项的格式

android - 如何检测Android出现静默故障的原因?

c# - 如何从 RichTextBox 中的鼠标单击点获取插入符位置?