全局异常处理
主程序 部分
 ThreadExceptionHandler handler =
 ThreadExceptionHandler handler =
 new ThreadExceptionHandler();
            new ThreadExceptionHandler();

 Application.ThreadException +=
            Application.ThreadException +=
 new ThreadExceptionEventHandler(
                new ThreadExceptionEventHandler(
 handler.Application_ThreadException);
                    handler.Application_ThreadException);
 //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(MainUIThreadExceptionHandler);
            //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(MainUIThreadExceptionHandler);

 //Application.EnableVisualStyles();
            //Application.EnableVisualStyles();
 //Application.SetCompatibleTextRenderingDefault(false);
            //Application.SetCompatibleTextRenderingDefault(false);
 //Application.Run(new Form1());
            //Application.Run(new Form1());
 System.Windows.Forms.Application.Run(new Form1());
异常处理部分
            System.Windows.Forms.Application.Run(new Form1());
异常处理部分
 internal class ThreadExceptionHandler
internal class ThreadExceptionHandler
 {
    {
 ///
        /// 
 /// Handles the thread exception.
        /// Handles the thread exception.
 ///
        /// 
 public void Application_ThreadException(
        public void Application_ThreadException(
 object sender, ThreadExceptionEventArgs e)
            object sender, ThreadExceptionEventArgs e)
 {
        {
 try
            try
 {
            {
 // Exit the program if the user clicks Abort.
                // Exit the program if the user clicks Abort.
 DialogResult result = ShowThreadExceptionDialog(
                DialogResult result = ShowThreadExceptionDialog(
 e.Exception);
                    e.Exception);

 if (result == DialogResult.Abort)
                if (result == DialogResult.Abort)
 Application.Exit();
                    Application.Exit();
 }
            }
 catch
            catch
 {
            {
 // Fatal error, terminate program
                // Fatal error, terminate program
 try
                try
 {
                {
 MessageBox.Show("Fatal Error",
                    MessageBox.Show("Fatal Error",
 "Fatal Error",
                        "Fatal Error",
 MessageBoxButtons.OK,
                        MessageBoxButtons.OK,
 MessageBoxIcon.Stop);
                        MessageBoxIcon.Stop);
 }
                }
 finally
                finally
 {
                {
 Application.Exit();
                    Application.Exit();
 }
                }
 }
            }
 }
        }
 ///
        /// 
 /// Creates and displays the error message.
        /// Creates and displays the error message.
 ///
        /// 
 private DialogResult ShowThreadExceptionDialog(Exception ex)
        private DialogResult ShowThreadExceptionDialog(Exception ex)
 {
        {
 string errorMessage =
            string errorMessage =
 "Unhandled Exception:\n\n" +
                "Unhandled Exception:\n\n" +
 ex.Message + "\n\n" +
                ex.Message + "\n\n" +
 ex.GetType() +
                ex.GetType() +
 "\n\nStack Trace:\n" +
                "\n\nStack Trace:\n" +
 ex.StackTrace;
                ex.StackTrace;

 return MessageBox.Show(errorMessage,
            return MessageBox.Show(errorMessage,
 "Application Error",
                "Application Error",
 MessageBoxButtons.AbortRetryIgnore,
                MessageBoxButtons.AbortRetryIgnore,
 MessageBoxIcon.Stop);
                MessageBoxIcon.Stop);
 }
        }
 }
    }
产生异常程序:
 private void AddWithUnhandledException()
 private void AddWithUnhandledException()
 {
        {
 txtName.Text = "Kevin";
            txtName.Text = "Kevin";
 throw new InvalidOperationException(
            throw new InvalidOperationException(
 "Invalid operation.");
                "Invalid operation.");
 }
        }      

 
From1.cs 代码如下:
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.ComponentModel;
using System.ComponentModel;
 using System.Data;
using System.Data;
 using System.Drawing;
using System.Drawing;
 using System.Text;
using System.Text;
 using System.Windows.Forms;
using System.Windows.Forms;
 using System.Threading;
using System.Threading;
 namespace WindowsApplication1
namespace WindowsApplication1
 {
{
 /// <summary>
    /// <summary>
 /// 全局异常处理函数
    /// 全局异常处理函数
 /// </summary>
    /// </summary>
 public partial class Form1 : Form
    public partial class Form1 : Form
 {
    {
 public Form1()
        public Form1()
 {
        {
 InitializeComponent();
            InitializeComponent();
 //Program.Breakstuff();
            //Program.Breakstuff(); 
 }
        }
 private void btnAdd_Click(object sender, EventArgs e)
        private void btnAdd_Click(object sender, EventArgs e)
 {
        {
 AddWithUnhandledException();
            AddWithUnhandledException();
 }
        }
 private void btnRemove_Click(object sender, EventArgs e)
        private void btnRemove_Click(object sender, EventArgs e)
 {
        {
 txtName.Clear();
            txtName.Clear();
 }
        }      
 ///
        /// 
 /// Adds default name to text box.
        /// Adds default name to text box.
 /// Throws unhandled exception.
        /// Throws unhandled exception.
 ///
        /// 
 private void AddWithUnhandledException()
        private void AddWithUnhandledException()
 {
        {
 txtName.Text = "Kevin";
            txtName.Text = "Kevin";
 throw new InvalidOperationException(
            throw new InvalidOperationException(
 "Invalid operation.");
                "Invalid operation.");
 }
        }      
 }
    }
 internal class ThreadExceptionHandler
    internal class ThreadExceptionHandler
 {
    {
 ///
        /// 
 /// Handles the thread exception.
        /// Handles the thread exception.
 ///
        /// 
 public void Application_ThreadException(
        public void Application_ThreadException(
 object sender, ThreadExceptionEventArgs e)
            object sender, ThreadExceptionEventArgs e)
 {
        {
 try
            try
 {
            {
 // Exit the program if the user clicks Abort.
                // Exit the program if the user clicks Abort.
 DialogResult result = ShowThreadExceptionDialog(
                DialogResult result = ShowThreadExceptionDialog(
 e.Exception);
                    e.Exception);

 if (result == DialogResult.Abort)
                if (result == DialogResult.Abort)
 Application.Exit();
                    Application.Exit();
 }
            }
 catch
            catch
 {
            {
 // Fatal error, terminate program
                // Fatal error, terminate program
 try
                try
 {
                {
 MessageBox.Show("Fatal Error",
                    MessageBox.Show("Fatal Error",
 "Fatal Error",
                        "Fatal Error",
 MessageBoxButtons.OK,
                        MessageBoxButtons.OK,
 MessageBoxIcon.Stop);
                        MessageBoxIcon.Stop);
 }
                }
 finally
                finally
 {
                {
 Application.Exit();
                    Application.Exit();
 }
                }
 }
            }
 }
        }
 ///
        /// 
 /// Creates and displays the error message.
        /// Creates and displays the error message.
 ///
        /// 
 private DialogResult ShowThreadExceptionDialog(Exception ex)
        private DialogResult ShowThreadExceptionDialog(Exception ex)
 {
        {
 string errorMessage =
            string errorMessage =
 "Unhandled Exception:\n\n" +
                "Unhandled Exception:\n\n" +
 ex.Message + "\n\n" +
                ex.Message + "\n\n" +
 ex.GetType() +
                ex.GetType() +
 "\n\nStack Trace:\n" +
                "\n\nStack Trace:\n" +
 ex.StackTrace;
                ex.StackTrace;

 return MessageBox.Show(errorMessage,
            return MessageBox.Show(errorMessage,
 "Application Error",
                "Application Error",
 MessageBoxButtons.AbortRetryIgnore,
                MessageBoxButtons.AbortRetryIgnore,
 MessageBoxIcon.Stop);
                MessageBoxIcon.Stop);
 }
        }
 }
    }
 }
}
 Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
 And the event handler looks like this.
And the event handler looks like this.
 public static void Application_UnhandledException(
public static void Application_UnhandledException(
 object sender, UnhandledExceptionEventArgs e)
    object sender, UnhandledExceptionEventArgs e)
 {
{
 // Handle exception.
  // Handle exception. 
 // The exception object is contained in e.ExceptionObject.
  // The exception object is contained in e.ExceptionObject.
 }
For ASP.NET applications you use the System.Web.HttpApplication.Error event which is placed in the Global.asax file. This might look something like:
}
For ASP.NET applications you use the System.Web.HttpApplication.Error event which is placed in the Global.asax file. This might look something like:
 protected void Application_Error(Object sender, EventArgs e)
protected void Application_Error(Object sender, EventArgs e)
 {
{
 Exception ex = Server.GetLastError();
    Exception ex = Server.GetLastError();

 // Stop error from displaying on the client browser
    // Stop error from displaying on the client browser
 Context.ClearError();
    Context.ClearError();

 Response.Write("Application_Error");
    Response.Write("Application_Error");
 Response.Write("Error Message: " + ex.ToString());
    Response.Write("Error Message: " + ex.ToString());
 }
}
 ThreadExceptionHandler handler =
 ThreadExceptionHandler handler = new ThreadExceptionHandler();
            new ThreadExceptionHandler();
 Application.ThreadException +=
            Application.ThreadException += new ThreadExceptionEventHandler(
                new ThreadExceptionEventHandler( handler.Application_ThreadException);
                    handler.Application_ThreadException); //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(MainUIThreadExceptionHandler);
            //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(MainUIThreadExceptionHandler);
 //Application.EnableVisualStyles();
            //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false);
            //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1());
            //Application.Run(new Form1()); System.Windows.Forms.Application.Run(new Form1());
            System.Windows.Forms.Application.Run(new Form1()); internal class ThreadExceptionHandler
internal class ThreadExceptionHandler {
    { ///
        ///  /// Handles the thread exception.
        /// Handles the thread exception. ///
        ///  public void Application_ThreadException(
        public void Application_ThreadException( object sender, ThreadExceptionEventArgs e)
            object sender, ThreadExceptionEventArgs e) {
        { try
            try {
            { // Exit the program if the user clicks Abort.
                // Exit the program if the user clicks Abort. DialogResult result = ShowThreadExceptionDialog(
                DialogResult result = ShowThreadExceptionDialog( e.Exception);
                    e.Exception);
 if (result == DialogResult.Abort)
                if (result == DialogResult.Abort) Application.Exit();
                    Application.Exit(); }
            } catch
            catch {
            { // Fatal error, terminate program
                // Fatal error, terminate program try
                try {
                { MessageBox.Show("Fatal Error",
                    MessageBox.Show("Fatal Error", "Fatal Error",
                        "Fatal Error", MessageBoxButtons.OK,
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        MessageBoxIcon.Stop); }
                } finally
                finally {
                { Application.Exit();
                    Application.Exit(); }
                } }
            } }
        } ///
        ///  /// Creates and displays the error message.
        /// Creates and displays the error message. ///
        ///  private DialogResult ShowThreadExceptionDialog(Exception ex)
        private DialogResult ShowThreadExceptionDialog(Exception ex) {
        { string errorMessage =
            string errorMessage = "Unhandled Exception:\n\n" +
                "Unhandled Exception:\n\n" + ex.Message + "\n\n" +
                ex.Message + "\n\n" + ex.GetType() +
                ex.GetType() + "\n\nStack Trace:\n" +
                "\n\nStack Trace:\n" + ex.StackTrace;
                ex.StackTrace;
 return MessageBox.Show(errorMessage,
            return MessageBox.Show(errorMessage, "Application Error",
                "Application Error", MessageBoxButtons.AbortRetryIgnore,
                MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
                MessageBoxIcon.Stop); }
        } }
    }产生异常程序:
 private void AddWithUnhandledException()
 private void AddWithUnhandledException() {
        { txtName.Text = "Kevin";
            txtName.Text = "Kevin"; throw new InvalidOperationException(
            throw new InvalidOperationException( "Invalid operation.");
                "Invalid operation."); }
        }      

From1.cs 代码如下:
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.ComponentModel;
using System.ComponentModel; using System.Data;
using System.Data; using System.Drawing;
using System.Drawing; using System.Text;
using System.Text; using System.Windows.Forms;
using System.Windows.Forms; using System.Threading;
using System.Threading; namespace WindowsApplication1
namespace WindowsApplication1 {
{ /// <summary>
    /// <summary> /// 全局异常处理函数
    /// 全局异常处理函数 /// </summary>
    /// </summary> public partial class Form1 : Form
    public partial class Form1 : Form {
    { public Form1()
        public Form1() {
        { InitializeComponent();
            InitializeComponent(); //Program.Breakstuff();
            //Program.Breakstuff();  }
        } private void btnAdd_Click(object sender, EventArgs e)
        private void btnAdd_Click(object sender, EventArgs e) {
        { AddWithUnhandledException();
            AddWithUnhandledException(); }
        } private void btnRemove_Click(object sender, EventArgs e)
        private void btnRemove_Click(object sender, EventArgs e) {
        { txtName.Clear();
            txtName.Clear(); }
        }       ///
        ///  /// Adds default name to text box.
        /// Adds default name to text box. /// Throws unhandled exception.
        /// Throws unhandled exception. ///
        ///  private void AddWithUnhandledException()
        private void AddWithUnhandledException() {
        { txtName.Text = "Kevin";
            txtName.Text = "Kevin"; throw new InvalidOperationException(
            throw new InvalidOperationException( "Invalid operation.");
                "Invalid operation."); }
        }       }
    } internal class ThreadExceptionHandler
    internal class ThreadExceptionHandler {
    { ///
        ///  /// Handles the thread exception.
        /// Handles the thread exception. ///
        ///  public void Application_ThreadException(
        public void Application_ThreadException( object sender, ThreadExceptionEventArgs e)
            object sender, ThreadExceptionEventArgs e) {
        { try
            try {
            { // Exit the program if the user clicks Abort.
                // Exit the program if the user clicks Abort. DialogResult result = ShowThreadExceptionDialog(
                DialogResult result = ShowThreadExceptionDialog( e.Exception);
                    e.Exception);
 if (result == DialogResult.Abort)
                if (result == DialogResult.Abort) Application.Exit();
                    Application.Exit(); }
            } catch
            catch {
            { // Fatal error, terminate program
                // Fatal error, terminate program try
                try {
                { MessageBox.Show("Fatal Error",
                    MessageBox.Show("Fatal Error", "Fatal Error",
                        "Fatal Error", MessageBoxButtons.OK,
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        MessageBoxIcon.Stop); }
                } finally
                finally {
                { Application.Exit();
                    Application.Exit(); }
                } }
            } }
        } ///
        ///  /// Creates and displays the error message.
        /// Creates and displays the error message. ///
        ///  private DialogResult ShowThreadExceptionDialog(Exception ex)
        private DialogResult ShowThreadExceptionDialog(Exception ex) {
        { string errorMessage =
            string errorMessage = "Unhandled Exception:\n\n" +
                "Unhandled Exception:\n\n" + ex.Message + "\n\n" +
                ex.Message + "\n\n" + ex.GetType() +
                ex.GetType() + "\n\nStack Trace:\n" +
                "\n\nStack Trace:\n" + ex.StackTrace;
                ex.StackTrace;
 return MessageBox.Show(errorMessage,
            return MessageBox.Show(errorMessage, "Application Error",
                "Application Error", MessageBoxButtons.AbortRetryIgnore,
                MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
                MessageBoxIcon.Stop); }
        } }
    } }
}
Asp.Net 的全局异处理函数
Console and ASP.NET Applications
For Console applications you should use the System.AppDomain.UnhandledException event. To hook it up you would write:
 Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
 public static void Application_UnhandledException(
public static void Application_UnhandledException( object sender, UnhandledExceptionEventArgs e)
    object sender, UnhandledExceptionEventArgs e) {
{ // Handle exception.
  // Handle exception.  // The exception object is contained in e.ExceptionObject.
  // The exception object is contained in e.ExceptionObject. }
} protected void Application_Error(Object sender, EventArgs e)
protected void Application_Error(Object sender, EventArgs e) {
{ Exception ex = Server.GetLastError();
    Exception ex = Server.GetLastError();
 // Stop error from displaying on the client browser
    // Stop error from displaying on the client browser Context.ClearError();
    Context.ClearError();
 Response.Write("Application_Error");
    Response.Write("Application_Error"); Response.Write("Error Message: " + ex.ToString());
    Response.Write("Error Message: " + ex.ToString()); }
} 
                    
                 
    
 
         
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号