1.WPF Global Catch ExceptionPublic partial class App:application {public App () {Occurs when an exception is thrown by an application but is not processed. The main point is the UI thread. This. Dispatcherunhandledexception + = new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler (app_ Dispatcherunhandledexception); Occurs when an exception is not captured. Main refers to non-UI threads AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (currentdomain_unhandledexception); } void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) { Can log and turn to error bug window friendly Prompt user if (E.exceptionobject is System.Exception) { Exception ex = (system.exceptio N) e.exceptionobject; MessageBox.Show (ex. Message); } } void APP_DISPATC Herunhandledexception (object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {//can log and turn to error bug window friendly Prompt user &NBsp e.handled = true; MessageBox.Show ("message:" +e.exception.message+ "\ r \ n "+e.exception.stacktrace); }2.winform catch Global exception Static class program { //<summary> &NBSP ; The main entry point for the application. //</summary> [stathread] Static void Main () { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); & nbsp Application.Run (new Form1 ()); Application.ThreadException + = Application _threadexception;////ui Thread exception AppDomain.CurrentDomain.UnhandledException + = currentdomain_unhandledexception;////Multithreading exceptions } //<summary>& nbsp //Multithreading exceptions //</summary> //<param NA Me= "Sender" ≫</param> //<param name= "E" ></param> static VO ID currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) { & nbsp //can log and turn to error bug window friendly Prompt user Exception ex = E.exceptionobj ECT as exception; MessageBox.Show (ex. Message); } //<summary> ///UI thread XOR Regular //</summary> //<param name= "Sender" ></param> ; //<param name= "E" ></param> static void application_t Hreadexception (object sender, System.Threading.ThreadExceptionEventArgs e) {// Can log and turn to error bug window friendly Prompt user MessageBox.Show (e.excEption. Message); } }}3.MVC program global catch Exception Namespace global exception capture test { public class mvcapplication:system.web.httpapplication { &NB Sp protected void Application_Start () { &NBSP;&NBSP;&NBSP ; Arearegistration.registerallareas (); Globalfi Lters. 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:filt Erattribute, iexceptionfilter { Public virtual void onexception (Exceptioncontext filtercontext) { & nbsp String message = String. Format ("Message type:{0}<br> message content:{1}<br> The method that throws the exception:{2}<br> throws the exception source: {3}" , FilterContext.Exception.GetType (). name , filtercontext.exception.message & nbsp &NBSP, filtercontext.exception.targetsite &NB SP;, FilterContext.Exception.Source + filtercontext.exception.stacktrace ); } }}4.web Form Global exception captureNamespace WebForm Global Exception capture {public class Global:System.Web.HttpApplication {protected void Application_Start ( Object sender, EventArgs e) {} void Application_Error (object sender, EventArgs e) {//code to run when an unhandled error occurs Exception objexp = HttpContext.Current.Server.GetLastError ();//Catch Global exception } }}
Those global exception captures in C #