全局异常处理

主程序 部分
 ThreadExceptionHandler handler =
            
new ThreadExceptionHandler();

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

            
//Application.EnableVisualStyles();
            
//Application.SetCompatibleTextRenderingDefault(false);
            
//Application.Run(new Form1());
            System.Windows.Forms.Application.Run(new Form1());
异常处理部分
internal class ThreadExceptionHandler
    
{
        
/// 
        
/// Handles the thread exception.
        
/// 

        public void Application_ThreadException(
            
object sender, ThreadExceptionEventArgs e)
        
{
            
try
            
{
                
// Exit the program if the user clicks Abort.
                DialogResult result = ShowThreadExceptionDialog(
                    e.Exception);

                
if (result == DialogResult.Abort)
                    Application.Exit();
            }

            
catch
            
{
                
// Fatal error, terminate program
                try
                
{
                    MessageBox.Show(
"Fatal Error",
                        
"Fatal Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Stop);
                }

                
finally
                
{
                    Application.Exit();
                }

            }

        }

        
/// 
        
/// Creates and displays the error message.
        
/// 

        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);
        }

    }

产生异常程序:
 private void AddWithUnhandledException()
        
{
            txtName.Text 
= "Kevin";
            
throw new InvalidOperationException(
                
"Invalid operation.");
        }
      


From1.cs 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
    
/// <summary>
    
/// 全局异常处理函数
    
/// </summary>

    public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
            
//Program.Breakstuff(); 
        }

        
private void btnAdd_Click(object sender, EventArgs e)
        
{
            AddWithUnhandledException();
        }

        
private void btnRemove_Click(object sender, EventArgs e)
        
{
            txtName.Clear();
        }
      
        
/// 
        
/// Adds default name to text box.
        
/// Throws unhandled exception.
        
/// 

        private void AddWithUnhandledException()
        
{
            txtName.Text 
= "Kevin";
            
throw new InvalidOperationException(
                
"Invalid operation.");
        }
      
    }

    
internal class ThreadExceptionHandler
    
{
        
/// 
        
/// Handles the thread exception.
        
/// 

        public void Application_ThreadException(
            
object sender, ThreadExceptionEventArgs e)
        
{
            
try
            
{
                
// Exit the program if the user clicks Abort.
                DialogResult result = ShowThreadExceptionDialog(
                    e.Exception);

                
if (result == DialogResult.Abort)
                    Application.Exit();
            }

            
catch
            
{
                
// Fatal error, terminate program
                try
                
{
                    MessageBox.Show(
"Fatal Error",
                        
"Fatal Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Stop);
                }

                
finally
                
{
                    Application.Exit();
                }

            }

        }

        
/// 
        
/// Creates and displays the error message.
        
/// 

        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);
        }

    }

}


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);
And the event handler looks like this.
public static void Application_UnhandledException(
    
object sender, UnhandledExceptionEventArgs e)
{
  
// Handle exception. 
  
// 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:
protected void Application_Error(Object sender, EventArgs e)
{
    Exception ex 
= Server.GetLastError();

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

    Response.Write(
"Application_Error");
    Response.Write(
"Error Message: " + ex.ToString());
}

posted @ 2007-03-21 15:38  jhtchina  阅读(1073)  评论(0)    收藏  举报