Those global exception captures in C #

Source: Internet
Author: User

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" &Gt;</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&gt ;       //<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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.