Error Handler for Forms rather than functions

Posted on 2008-11-13
Last Modified: 2012-05-05

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.
Question by:RobKearney
    LVL 46

    Accepted Solution

    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


    LVL 2

    Assisted Solution

    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


    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now