0

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!

2 Answers2

0

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.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Note that it is not safe to keep the application running after an unexpected exception is thrown. That event handler does let you do so but it is inadvisable. If you don't prevent it, the application will shut down but it will do so cleanly. Another option is to cancel the automatic exit and then call `Application.Restart`, to automatically get your user back into the application without having to restart it manually. I have done this before with a `MessageBox` showing `AbortRetryIgnore` buttons. Abort = exit, Retry = restart, Ignore = continue. The client insisted on that last option. – jmcilhinney May 29 '20 at 03:52
-1

Regarding your emailing idea, just sure to add in a privacy notice before auto-emailing yourself anything in your apps; this can be a big bone of contention to users, & if an astute one catches it silently phoning home your rep is down the drain.

As for a global error handler, have a look here:

https://www.dotnetcurry.com/patterns-practices/1364/error-handling-dotnet-projects

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](//stackoverflow.com/help/deleted-answers). – Ken White May 27 '20 at 22:17