VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 11

Inline Error Handling

Another form of the On Error statement is On Error Resume Next. This alternative is used to handle an error immediately after the line causing the error rather than branching to a specified error handler. Another use of On Error Resume Next is to simply allow your application to ignore errors that you don't need to handle at all.

In Listing 11.1, you can assume that the GetObject function might cause a runtime error. Notice that this code attempts to handle the error on the following line by first evaluating the current value of Err.Number.


Private Sub XLInLine()
   Dim errTemp As Long
   On Error Resume Next
   Set xl = GetObject(, "Excel.Application")
   Select Case Err.Number
      Case 0
         MsgBox "Successfully Opened Excel"
      Case 429
         Set xl = CreateObject("Excel.Application")
      Case Else
         MsgBox "Fatal error #" & Err.Number _
         & " (" & Err.Description & ")" & _
         ": Passing the buck"
   End Select
End Sub

The example also checks to see if the value of Err.Number is 0, which indicates that no error has occurred.

Notice that the Err object's Clear method is called after the Select Case structure handles the error. You need to call Err.Clear because inline error handling doesn't use the Resume statement, and Resume is what you normally rely on to reset the Err object. If an error had occurred in the code in Listing 11.1 and Err.Clear hadn't been used, the Err object would continue to store information about this error.

This could mislead a called procedure, which might have its own error handlers and would depend on the value of Err.Number to function properly.

Remember to call Err.Clear after each place where, in a routine where you handle inline errors, an error could occur in any of these locations, and you must handle each one individually. Generally, you'll want to be careful about using inline error handling. Your procedures can get quite long and more difficult to construct and maintain since an error handler must follow each line that could cause an error.

As we mentioned previously, the On Error Resume Next statement is used for inline error handling. There are other times, however, when the On Error Resume Next statement comes in handy. There are basically two uses for On Error Resume Next:

  • Ignore errors and keep on processing. This is useful when you might expect an error to occur occasionally, but the error does not affect the remaining code. For example, the code in Listing 11.2 will process all the controls on the current form.

    LISTING 11.2

    On Error Resume Next
    For each ctrlCurr In Controls
       ctrlCurr.Text = UCase(ctrlCurr.Text)
    Next ctrlCurr
  • Whenever the loop hits a control without a Text property, such as a Label, VB generates a runtime error. In Listing 11.2, the error is not relevant and does not require handling because you want VB to simply ignore any control that does not apply (controls without a text property). The On Error Resume Next statement tells the system to ignore the error and keep going.

  • Process errors immediately after they occur. This is possible because VB sets the values of Err.Number and Err.Description when an error occurs even if On Error Resume Next is in effect. This enables you to use a second style of local error trapping— inline error handling—as previously discussed in this section. Inline error handling therefore offers you an alternative, and often less cumbersome, way to process errors in a routine.


<< Previous | Contents | Next (Chapter 12) >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved