Error Handler for Forms rather than functions


We can add an error handler to any of the functions routines, but what we are looking for is can we add a Error handler to a whole form.

The reason we want this we get a the same error message for many functions and would like to create a generic consolidated error handler for them.
Who is Participating?
Wayne Taylor (webtubbs)Connect With a Mentor Commented:
Hi RobKearney,

You can add a project wide exception catcher....

1) Go to Project Properties > Application tab
2) Ensure "Enable application framework" is checked.
3) Click the "View Application Events" button
4) Then in the "MyApplication" class, you can use this event....

    Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
    End Sub


MTEXXConnect With a Mentor Commented:
Here's a way to do it.  I think there are much better ways; not cookie-cutter stamped into each class but rather a singleton log / error handler.  If it is needed to occur in a form, then you must have very specific business logic to deal with the errors, which would not be reusable.  Hence, it is custom code and there are really not any canned solutions.

At any rate- as you asked:
    ''' <summary>
    ''' Class-level exception handler.  Paste this in each class, and hardcode the MyClass name
    ''' </summary>
    ''' <param name="functionName">The name of the function generating the exception</param>
    ''' <param name="culprit">The offending function call and arguments</param>
    ''' <param name="ex">The exception caught.</param>
    ''' Example: in a try-catch block, 'Catch ex As Exception: handleExceptions("myFunctionName", "", ex)
    ''' <remarks></remarks>
    Private Sub handleExceptions(ByVal functionName As String, ByVal culprit As String, ByVal ex As Exception)
        'TODO mirror this to you log file, windows event log etc...
        Console.WriteLine("EXCEPTION in MyClass::" + functionName + "(" + culprit + ") " + ex.ToString)
    End Sub

Open in new window

RobKearneyAuthor Commented:
Kinda lost my focus on this, but as always am more than grateful for posts.

Can't confirm if solution for future searchers, but split points in recognition of posting in first place
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.