winForm:面对未捕获的异常时

43 篇文章 1 订阅

还记得某牛人说过的,代码越多,存在的BUG就可能越多,面对BUG,我们只能通过我们的细心,经验去避开,去解决,但面对一些未曾料到的BUG而抛出异常时,我们未能将其捕获时,软件经常无理由地弹出一个警告框而退出,甚至于久违的蓝屏,我们真的无能为力了呀!

或许我们能做点什么。。。。。。。

据msdn记载

 

The following code example sets event handlers for exceptions that occur on Windows Forms threads and exceptions that occur on other threads.

 

It setsSetUnhandledExceptionMode so that all exceptions are handled by the application, regardless of the settings in the application's user configuration file.

 

It uses the ThreadException event to handle UI thread exceptions, and the UnhandledException event to handle non-UI thread exceptions. SinceUnhandledException cannot prevent an application from terminating, the example simply logs the error in the application event log before termination.

像GOOGLE的crome一样,一打开即是崩溃的信息。。。。。。但我们知道是那错了,或者像WEB迅雷一下,自动重启一下,而这些在Winform中,即是能过上面所描述的而捕获

 

        public UnhandledExceptionDlg()

        {

            // Add the event handler for handling UI thread exceptions to the event:

            Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);

 

            // Set the unhandled exception mode to force all Windows Forms errors to go through our handler:

            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

 

            // Add the event handler for handling non-UI thread exceptions to the event:

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);

        }

 

        /// <summary>

        /// Handle the UI exceptions by showing a dialog box

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void ThreadExceptionFunction(Object sender, ThreadExceptionEventArgs e)

        {

            // Suppress the Dialog in Debug mode:

            #if !DEBUG

            ShowUnhandledExceptionDlg(e.Exception);

            #endif

        }

 

        /// <summary>

        /// Handle the UI exceptions by showing a dialog box

        /// </summary>

        /// <param name="sender">Sender Object</param>

        /// <param name="args">Passing arguments: original exception etc.</param>

        private void UnhandledExceptionFunction(Object sender, UnhandledExceptionEventArgs args)

        {

            // Suppress the Dialog in Debug mode:

            #if !DEBUG

            ShowUnhandledExceptionDlg((Exception)args.ExceptionObject);

            #endif

        }

 

        /// <summary>

        /// Raise Exception Dialog box for both UI and non-UI Unhandled Exceptions

        /// </summary>

        /// <param name="e">Catched exception</param>

        private void ShowUnhandledExceptionDlg(Exception e)

        {

            Exception unhandledException = e;

 

            if(unhandledException == null)

                unhandledException = new Exception("Unknown unhandled Exception was occurred!");

 

 

我想作者的话已经很明确清楚了。。。。。。。

 


然后返回给我们的信息,通过什么方式传都可以,此时再把相关资源释放掉,退出系统

 

 

private DialogResult ShowThreadExceptionDialog(Exception ex) 
    {
        string errorMessage= 
            "Unhandled Exception:\n\n" +
            ex.Message + "\n\n" + 
            ex.GetType() + 
            "\n\nStack Trace:\n" + 
            ex.StackTrace;
 
        return MessageBox.Show(errorMessage, 
            "Application Error", 
            MessageBoxButtons.AbortRetryIgnore, 
            MessageBoxIcon.Stop);
    }

 

 

对我们来讲,或许这里面最重要的信息就是Stack Trace  嘿嘿!接着就是重启了。。。。

// User wants to restart the App:

if(ar.RestartApp)

{

Console.WriteLine("The App will be restarted...");

System.Diagnostics.Process.Start(System.Windows.Forms.Application.ExecutablePath);

}


补充点。


System.Windows.Forms.Application类

提供 static 方法和属性以管理应用程序,例如启动和停止应用程序、处理 Windows 消息的方法和获取应用程序信息的属性。

 

System.Windows.Forms.Application.ThreadException 事件

在发生未捕获线程异常时发生。

 

System.Windows.Forms.Application.SetUnhandledExceptionMode()方法

指示应用程序如何响应未处理的异常。

  • SetUnhandledExceptionMode(UnhandledExceptionMode)

    指示应用程序如何响应未处理的异常。

  • SetUnhandledExceptionMode(UnhandledExceptionMode, Boolean)

    指示应用程序如何响应未处理的异常,同时可选择应用特定于线程的行为。

 

System.Windows.Forms.UnhandledExceptionMode枚举

定义 Windows 窗体应用程序应在何处发送未处理的异常。

public enum UnhandledExceptionMode

{

Automatic,        //将所有异常都传送到 ThreadException 处理程序,除非应用程序的配置文件指定了其他位置。

ThrowException,    //从不将异常传送到 ThreadException 处理程序。忽略应用程序配置文件。

CatchException        //始终将异常传送到 ThreadException 处理程序。忽略应用程序配置文件。


转载地址:http://www.cnblogs.com/yellowyu/archive/2008/09/04/1284003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值