Sub that catches all errors

Sandallic

Newcomer
Joined
Dec 10, 2004
Messages
11
Hello

I got this little problem, Im working with an windowsapplication.
Whenever an error occures the program must close and no errors are allowed to stop this program.

I have been putting "try...catch" within all my procedures, but it seems like it wants to be error messageboxes anyway.

Is there anyway I can have one sub that only runs when error occures ?
Like my page_load sub, that "handles mybase.Load"?

Thx, Sandallic
 
On way is to handle the Appdomain's UnhandledException event

Visual Basic:
    Public Shared Sub Main()

        'setup global error handler
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AllErrorsCaughtHere

        Dim f As New Form1
        Application.Run(f)
    End Sub

    Private Shared Sub AllErrorsCaughtHere(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        'deal with errors here

    End Sub
I would still continue to use try catch blocks within procedures as it makes more sense to handle errors specifically whenever possible; the above code will just catch unhandled errors - the problem is you are then potentially keeping yur application alive but no longer know the state of various objects within it due to the previous error.
 
Re:

You got this line,

Code:
Dim f as new Form1
Application.Run(f)

That makes a new instance of my Form, Do I have to do that ? Or is it enough
with:

Code:
    Public Shared Sub Main()

        'setup global error handler
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AllErrorsCaughtHere

    End Sub

    Private Shared Sub AllErrorsCaughtHere(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        'deal with errors here

    End Sub
 
Back
Top