VB.net capturing a project global exception

Source: Internet
Author: User

Add the following code to your project: Create a new window to display the exception information.

NamespaceMy'Global error handling, new solutions add the ApplicationEvents.vb directly to the project    'You will also need a from to display the error after you add it. If we don't get to this stage, we need to lay the groundwork.    '========================================================    'The following events are available for MyApplication:    'start: Raised before the startup form is created when the application starts.     'closed: All application forms are closed upon request. If the application terminates abnormally, this event is not raised.     'UnhandledException: Thrown if the application encounters an unhandled exception.     'StartupNextInstance: Raised when a single-instance application is started and the application is already active.     'networkavailabilitychanged: Raised when a network connection is connected or disconnected.     Partial Friend ClassMyApplication'One of the global exceptions we caught is not thread safe, so we need to make it thread safe first.         Private Delegate SubSafeapplicationthreadexception (ByValSender as Object,ByValE asThreading.threadexceptioneventargs)Private SubShowdebugoutput (ByValEx asException)'Show output error from form            DimFrmd as NewForm2 ()'Display Solution NameFrmD.TextBox1.AppendText ("Product Name:"& My.Application.Info.ProductName &vbnewline)'Show current version numberFrmD.TextBox1.AppendText ("Product Version:"& My.Application.Info.Version.ToString () &vbnewline) FrmD.TextBox1.AppendText ("OS Name:"& My.Computer.Info.OSFullName &vbnewline) FrmD.TextBox1.AppendText ("OS Version:"& My.Computer.Info.OSVersion &vbnewline)'NET2.0 Judge x86 or a            IfEnvironment.getenvironmentvariable ("ProgramFiles (x86)") =""  ThenFrmD.TextBox1.AppendText ("OS platform:x86"&vbnewline)ElseFrmD.TextBox1.AppendText ("OS platform:x64"&vbnewline)End IfFrmD.TextBox1.AppendText ("--------------------"&vbCrLf)'Display ErrorFrmD.TextBox1.AppendText (ex. ToString ()) Frmd.showdialog ()'Performing application Cleanup            'TODO: Add your application cleanup code here.             'exit the application-or try to recover from the exception:Environment.exit (0)        End Sub        Private SubMyapplication_startup (ByValSender as Object,ByValE asMicrosoft.VisualBasic.ApplicationServices.StartupEventArgs)Handles Me. Startup'There are three places to catch all the global unhandled exceptions:            'AppDomain.CurrentDomain.UnhandledException event.             'System.Windows.Forms.Application.ThreadException event.             'myapplication.unhandledexception event.             AddHandlerAppDomain.CurrentDomain.UnhandledException,AddressOfappdomain_unhandledexceptionAddHandlerSystem.Windows.Forms.Application.ThreadException,AddressOfapp_threadexceptionEnd Sub        Private SubApp_threadexception (ByValSender as Object,ByValE asThreading.threadexceptioneventargs)'This is not thread-safe, so make it thread safe.             IfMainform.invokerequired Then                'calling the main threadMainform.invoke (NewSafeapplicationthreadexception (AddressOfApp_threadexception),New Object() {sender, E})Elseshowdebugoutput (e.exception)End If        End Sub        Private SubAppdomain_unhandledexception (ByValSender as Object,ByValE asUnhandledExceptionEventArgs) Showdebugoutput (DirectCast(E.exceptionobject, Exception))End Sub        Private SubMyapplication_unhandledexception (Sender as ObjectE asMicrosoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)Handles Me. UnhandledException showdebugoutput (e.exception)End Sub    End ClassEnd Namespace

VB.net capturing a project global exception

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.