BCarpe
BCarpe

Reputation: 870

Universal Exception Handler

Is there a simple way to catch all exceptions in your VB.NET applications? I'm interested in making it so that instead of my users seeing a runtime error, they just are told that an error occurred and to contact tech support, then the relevant error information is logged so our support team can look at it afterwards.

Upvotes: 1

Views: 1723

Answers (4)

miroxlav
miroxlav

Reputation: 12194

If you have forms application, it is not practical to have the handler around Application.Run() as the only event handler. Keep it there, but add also two others:

When inside the form, and exception occurs, you want to keep execution in that context – keep the form open etc. For this purpose, use ThreadExceptionEventHandler.

And also, for case when application is irrepairably crashing on some problem, add handler to Dispatcher.UnhandledException event. It is last thing executed before the application ends. You can log the exception to disk for later investigation etc. Very useful.

Let's see some good resource how they are applied.

Upvotes: 0

Josh Part
Josh Part

Reputation: 2164

You can use the OnUnhandledException application event to catch (almost) every exception that wasn't handled by the code.

On the Project Properties window (double-click project file on the solution explorer or Project Menu -> [Project name] properties), the Application page has a "View Application Events" button that creates a new file in your project.

application event button

In that file there are some events that are fired at application level; one of those is the UnhandledException. Whatever you put there will be executed instead of the classic JIT dialog. The UnhandledExceptionEventArgs object has an Exception property with the unhandled exception object, and a ExitApplication property that decides if the application should exit or continue executing.

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
        Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            MessageBox.Show(e.Exception.ToString) '<-- the exception object
            e.ExitApplication = True '<-- True if you want the application to close; false to continue - if it can
        End Sub
    End Class

End Namespace

Note that there are still some "über-exceptions" that can't be caught even with this method (out of memory for example), but this way you can log what exceptions are not being correctly handled on your code, or what scenarios are actualy happening that weren't considered at the beggining.

More info here

As a side note: don't rely too much on this event. This has to be for extremely exceptional cases, as what is caught here should be treated ASAP in the corresponding class/module/method. This is a helpful tool for debugging and test cases but having too much exceptions being handled by this event would be a sign of something wrong in your code.

Upvotes: 5

Martin Soles
Martin Soles

Reputation: 559

It depends on what environment your application is running in. If you are using WPF or WinForms, you would launch your application using a main method instead of directly launching a form or page. Your main method should then wrap the calls to instantiate the UI in a try catch block.

So, for a WinForms Application, you could do something like this:

Sub Main
  Try
    Dim MainUI As New Form1
    MainUI.Show()
    Application.Run
  Catch ex As Exception
    'Do that fancy exception processing
  End Try
End Sub

You can do something similar with WPF. But, WPF also supports an event model where you are notified of exceptions, very similar to the one that ASP.Net uses.

Upvotes: 1

Isolin
Isolin

Reputation: 876

You will never be able to catch the StackOverflowException.

All the others for sure yes. I'm not familiar with VB, but it is easy to achieve in C#. For VB, I think the generic exeption handler could be

Try
    ...
Catch e As Exception
    ...
End Try

Of course this has to wrap all your code. You can find more for examples here.

Upvotes: 0

Related Questions