VBCE.com - The Premier Website For Visual Basic/CE Developers

 

*Dev Corner

Sample Code
Controls
Workarounds
Tips & Tricks
Q & A
Forums

*Goodies
Downloads
Software
Bookstore


*General
Home
What's New
General Info
Misc. Info


*News Worthy
News
Articles
Editorials
KB Articles
Reviews
Awards

*Miscellaneous
Web Links
Partners
Search
Feedback
Advertising

<% On Error Resume Next SiteStats() %>

Sponsors


More...


Incorporating Error Handling in VBCE
By webmaster@vbce.com

Error handling is a bit different in VBCE than in regular Visual Basic. Because the program flow in VBCE is basically VBScript, there are many error handling techniques that you would normally use in Visual Basic that you cannot use in VBCE. Examples of items that are not available in VBCE are On Error Goto MyErrorHandler, Resume, and Resume Next. Furthermore, runtime errors are not displayed the same in VBCE. If an unhandled error occurs in VBCE, you simply get the following error message:

An error was encountered while running this program.

The following code shows this error:

    Private Sub Command1_Click()
        'Raise an error
        Err.Raise 429
    End Sub

Normally in Visual Basic you would get the following error:

error1a.jpg (16225 bytes)

There is one advantage to this, and that is that the program does not terminate like a standard Visual Basic application would. The drawback is that there is no indication of what the error was so that the user can report it, and it's going to bring error handling to be nearly an art form when it comes to VBCE.

VBCE has only one method of handling errors, On Error Resume Next. To ensure that the errors are reported, you should add at least the following code to each procedure that you feel has any chance of failing:

    Private Sub Command1_Click()
        On Error Resume Next

        'Raise an error
        Err.Raise 429
        'Report the last error
        If Err.Number <> 0 Then
            MsgBox "An error has occurred in the Command1_Click Event:" & vbNewLine _
            & Err.Number & ": " & Err.Description, vbCritical, _
            "Program Error"
        End If
    End Sub

This code sample above would produce the following message:

error2.jpg (11789 bytes)

Although this does report if any error occurred in the procedure, it has one drawback - it only reports the last error number, which may be irrelevant because the first error that occurred may have caused the second error. The first error goes unreported, but at least you know what routine is generating an error.

Things brings about the need for a reusable error reporting and checking routine. The code below is called to check for an error. If an error has occurred, it is reported and the user has the chance to end the application, or continue:

    Private Sub Command1_Click()
        On Error Resume Next
        'Raise an error
        Err.Raise 429
        'If an error occurs we just report it
        ReportError "Command1_Click"
        'if an error occurs we do not continue the sub
        Err.Raise 438
        If ReportError("Command1_Click") Then Exit Sub
        MsgBox "Sub code Complete"
    End Sub

    Public Function ReportError(ProcedureName)
        '==================================
        'Displays error message and returns
        'Error number if there was an error
        '==================================

        Dim iMsgboxAnswer
        ReportError = Err.Number
        If Err.Number <> 0 Then
            iMsgboxAnswer = MsgBox("An error has occurred in " _
            & ProcedureName & ":" & vbNewLine & vbNewLine _
            & Err.Number & ": " & Err.Description & vbNewLine & vbNewLine & _
            "Continue Program? (Choosing No will end the program.)", _
            vbCritical + vbYesNo, "Program Error")
            Err.Clear
            Select Case iMsgboxAnswer
                Case vbYes: Exit Function
                Case vbNo: App.end
            End Select
        End If
   End Function

The code above generates the following dialog:

error3.jpg (16974 bytes)

If the user chooses Yes, the program continues, if the user chooses No the program ends. This function can be used two ways:

1) Just show the error and continue on yes, as shown in the error 429 that we generated.

2) Show the error and then make a program flow decision based on whether there was an error or not, as shown in the error 438 that we generated.

I'm sure it will take some getting used to, but with a little practice you'll be handling VBCE program errors like a pro. Yes, it will take a bit more coding to implement full error handling, but much of the code can be copied and pasted throughout your program. If you wish to implement this by massive copy-and-paste, you may wish to just call the ReportError routine with the name of your application instead of the procedure name, like this:

    If ReportError("Hello World App") Then Exit Sub

If you have implemented a different error handling technique, be sure to let me know.

webmaster@vbce.com

 

VBCE.com is DevX Winner!

Unless otherwise noted, all information on VBCE.com is Copyright 1998 - 2002
Windows, Windows CE, and Visual Basic are trademarks of the Microsoft Corporation.
VBCE.com is not responsible for content on external sites.
Send all feedback to webmaster@vbce.com
Webmasters - feel free to link to
VBCE.com - Premier Website for Visual Basic/CE Development

Buy Books!