C#中的那些全局異常捕獲


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(); //捕獲全局異常
          
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM