A Brief Review of Error Handling in VB6


For compatibility, Visual Basic 2005 and other .NET versions of Visual Basic still support the old-style syntax for error handling that was used in Visual Basic 6 and earlier versions. That means you can still use the syntax presented in this review. However, it is strongly recommended that you avoid using this old-style syntax in favor of the exception handling features that are native to .NET. Using the new Try...Catch syntax (presented after this review) will give you more flexibility and better code structure.

The old-style syntax in VB6 was handed down from DOS versions of BASIC. The On Error construct was created in an era when line labels and GoTo statements were commonly used. Such error handling is difficult to use and has limited functionality compared to more modern alternatives.

In VB6, a typical routine with error handling code looks like this:

  Private Function OpenFile(sFileName As String) As Boolean On Error GoTo ErrHandler: Open sFileName For Random As #1 OpenFile = True Exit Sub ErrHandler: Select Case Err.Number     Case 53 ' File not found         MessageBox.Show "File not found"     Case Else          MessageBox.Show "Other error" End Select OpenFile = False  End Function 

The top of the routine points to a section of code called an error handler, which is usually placed at the bottom of the routine. The error handler takes control as soon as an error is detected in the routine, and it checks the error number to determine what action to take. The error number is available as a property of the Err object, which is a globally available object that holds error information in VB6.

There are several other error handling syntax options not included in the preceding error-handling code. If the error handler can take care of the error without breaking execution, then it can resume execution with the line of code that generated the error ( Resume), the one after that ( Resume Next), or at a particular location ( Resume {LineLabel}).

Error handling code becomes more complex if the error handling needs to vary in the routine. Multiple On Error GoTo statements must be used to send errors to various error handlers, all of which are clustered at the bottom of the routine. With such a lack of organization of error handling code, it is easy to become confused about what should happen under various conditions. There is also very little information available about the error during the process, except for the error number. You can’t tell, for example, the line number on which the error was generated without single-stepping through the code.

Such logic can rapidly become convoluted and unmanageable. There’s a much better way to manage errors in VB 2005, called structured exception handling. The rest of this chapter discusses this new way to work with code errors, and uses the term structured exception handling throughout, except for the small sections that discuss compatibility with older error-handling techniques.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net