Search code examples
vb.nettry-catchvb.net-2010

VB.NET catch error for ENTIRE application


Need help with some VB.net coding (NOT C or c++) So far I'm using this code to catch errors for specific line(s) of codes:

Try
    bla bla bla                   
Catch ex As Exception
    msbox("Error: " & ex.message)
End Try

But sometimes the application stops due to an error where I don't have a catch; how do I on occasions like this call upon a specific Sub (catch the error) for ANY OTHER error in the ENTIRE application where the Sub will display the error message (where I also plan on sending my self an e-mail in that sub to notify me application has stopped)?

I'm not sure if it will conflict with all current Try/Catch commands in my application, but I would prefer to only catch the error on code that currently is not within a Catch handler.

Thank you so much!


Solution

  • This functionality is built into the VB application framework, which is enabled by default in WinForms applications. Open the Application page of the project properties, click the View Application Events button and add a handler for the UnhandledException event using the navigation bar at the top of the code window. Done!

    The VB application framework hides some of the complexity of applications that you must implement yourself in C#. It includes a Startup event that you can handle instead of adding code to a Main method, a StartupNextInstance event to support single-instance apps with commandline arguments, multi-threaded splash screen functionality and more.