异常实现

作者在 2008-04-12 13:38:37 发布以下内容

public class AppException : Exception

    {

        public AppException()

        {

        }

        public AppException(string message)  : base(message)//注意这里

        {

        }

        public AppException(object invalidData, string message)

            : base(message + "\n数据:" + invalidData.ToString())//注意这里

        {

        }

}

单是以上的代码还不行,这里虽然可以做到重新定义了异常,但是当调试时遇到异常是跳不过去的,还要将Main函数作修改。代码如下:

static void Main()

 {

    CustomExceptionHandler eh = new CustomExceptionHandler();

    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(eh.OnThreadException);

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new WinForm());

}

#region 使主程序捕获全局异常后弹出错误的提示

        internal class CustomExceptionHandler

        {

            public void OnThreadException(object sender, System.Threading.ThreadExceptionEventArgs t)

            {

                DialogResult result = DialogResult.Cancel;

                try

                {

                    result = this.ShowThreadExceptionDialog(t.Exception);

                }

                catch

                {

                    throw;

                }

            }

            private DialogResult ShowThreadExceptionDialog(Exception e)

            {

                return MessageBox.Show(e.Message.ToString());

            }

        }

#endregion

 

常用 | 阅读 2417 次
文章评论,共0条
游客请输入验证码
浏览24980次