Error Handling

By the end of the physical stage, you must have documented all of the error statements for your Visual Basic application. The possible errors for your application can be found in the constraint business rules and the alternative flows of the use cases.

Visual Basic allows you to use error numbers ranging from 512 to 32768. You should give each of your components their own range of numbers. If each of your components has 100 possible user-defined errors, this will allow you to make over 300 applications with no conflict of the error numbers. If you set a range of numbers for each component, then you will know exactly which component raised an error when an error occurs in your application. This will make debugging much easier, especially when there are dozens of components that make up the application. For the situations where the same error message may be presented in many different components, you should create a standard for the message, but each component should have their own number assigned to this standard message.

Visual Basic now allows you to use resource files. Resource files are a great place to store error messages. To use a resource file, you will need to add a method into your Visual Basic application to retrieve the string associated with an error. Let us look at how this is done.

In Visual Basic, go to the menu and select Add-Ins|Add-In Manager. The Add-in manager will come up. Double click the VB 6 Resource Editor:

Add-In Manager

Available Add-lns

| Load Behavior

3

Source Code Control

Startup / Loaded

VB 6 ActiveX Ctrl Interface Wizard

VB 6 ActiveX Doc Migration Wizard VB 6 Add-In Toolbar

VB 6 API Viewer

VB G Application Wizard VB 6 Class Builder Utility

VB 6 Data Form Wizard

VB 6 Data Object Wizard

VB 6 Property Page Wizard

IVB £ Resource Editor

Loaded

VB 6 Template Manager wn r- ^, /: . j k j _..

Help

J sci f: 'lui

Visual Basic G Resource Editor

Loaded/Unloaded r~ Load on Startup r~ Command Line

Load Behavior

Loaded/Unloaded r~ Load on Startup r~ Command Line

Click OK. Now you can edit resource files in Visual Basic. Go to the menu and select ToolsjResource Editor to open up the resource editor.

Click on the abc on the toolbar to open the string editor. You can assign each error number to a string that gives a description of the error. It could look as follows:

Once you have created your error message strings, you must save them using the VB Resource Editor. The .res file that is created can be shared by all of the components in your project. This .res file should be made during the physical stage.

You can create a function that uses the LoadResString Visual Basic function to retrieve the correct error string:

Public Function GetErrorText(ByVal v lngErrorNumber As

Long)

as String

On Error GoTo GetErrorText Error

GetErrorText = LoadResString(v lngErrorNumber)

Exit Function

GetErrorText Error:

GetErrorText = "An unknown error has occurred, the End If

error

was not found"

End Function

If the error number is not in the resource file, it will return the default unknown error message. Just to make things a little easier for the Visual Basic programmer, we can make an enumerated type with the error messages. This way the developer does not have to remember all of the error numbers. The enumerated type would look as follows:

Private Enum ErrorCollection e UserMustLogin = 2000 e_UserEnteredInvalidID = 2001 e UserEnteredInvalidPassword = 2002 e CanNotDeleteCustomerPendingOrder = 2003 End Enum

These enumerated types should also be made in the physical stage. This type would look as follows to a developer if they hit control-space bar and typed the letter e_ :

This makes it easy to find the correct error. I do realize this is a bit different than the way most error messages are created. For the most part, developers start coding and as they find they need an error message they add one to the list. This can lead to duplicate numbers and a lack of uniformity in the numbering and content of the error messages.

By defining the errors in the physical stage and designing your technique of coding with activity diagrams before the code is written, developers will know exactly where errors should be placed and which error should be placed there. This is a much cleaner and easier way to code.

One final comment on error messages: try to word your error messages in a way that any user can understand them and understand what has gone wrong. Try to build some intelligence into your applications so that when an error occurs, not only does the user get an error message, but also suggestions on how they may correct the error. Doing this will substantially reduce the number of help desk calls.

Was this article helpful?

0 0
365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Post a comment