1.WPF全局捕獲異常
public partial class App : Application
{
public App()
{
// 在異常由應用程序引發但未進行處理時發生。主要指的是UI線程。
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
// 當某個異常未被捕獲時出現。主要指的是非UI線程
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//可以記錄日志並轉向錯誤bug窗口友好提示用戶
if (e.ExceptionObject is System.Exception)
{
Exception ex =(System.Exception)e.ExceptionObject;
MessageBox.Show(ex.Message);
}
}
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//可以記錄日志並轉向錯誤bug窗口友好提示用戶
e.Handled = true;
MessageBox.Show("消息:"+e.Exception.Message+"\r\n"+e.Exception.StackTrace);
}
2.winform捕獲全局異常
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.ThreadException += Application_ThreadException;////UI線程異常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;////多線程異常
}
/// <summary>
/// 多線程異常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//可以記錄日志並轉向錯誤bug窗口友好提示用戶
Exception ex = e.ExceptionObject as Exception;
MessageBox.Show(ex.Message);
}
/// <summary>
/// UI線程異常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
//可以記錄日志並轉向錯誤bug窗口友好提示用戶
MessageBox.Show(e.Exception.Message);
}
}
}
3.MVC程序全局捕獲異常
namespace 全局異常捕獲測試
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new CustomExceptionAttribute());
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{
public virtual void OnException(ExceptionContext filterContext)
{
string message = string.Format("消息類型:{0}<br>消息內容:{1}<br>引發異常的方法:{2}<br>引發異常源:{3}"
, filterContext.Exception.GetType().Name
, filterContext.Exception.Message
, filterContext.Exception.TargetSite
, filterContext.Exception.Source + filterContext.Exception.StackTrace
);
}
}
}
4.web form全局異常捕獲
namespace WebForm全局異常捕獲
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時運行的代碼
Exception objExp = HttpContext.Current.Server.GetLastError(); //捕獲全局異常
}
}
}