WPF UnhandledException阻止程序奔溃

WPF程序捕获全局异常,遇到未处理的异常,虽可以捕获,但是不能阻止程序退出。

 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
   void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is System.Exception)
            {
                Exception ex = (System.Exception)e.ExceptionObject;
                MessageBox.Show(ex.Message);
            }
           
        }

只要在App.Config文件中添加

 <runtime>

      <legacyUnhandledExceptionPolicy enabled="1"/>
</runtime>

猜你喜欢

转载自www.cnblogs.com/czly/p/12124387.html
WPF