Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
From the author of

Structure Exception Handling

Error handling in VB is a drag. The fundamental technique you need in order to trap and address errors came over on the boat with Columbus—it’s that old! It’s based on the archaic On Error GoTo SomeLineLabel syntax, as shown below:

Private Sub MakeError()
    On Error GoTo FuncErr:
    
    Dim i As Integer
    
    i = 45600000000000.3
    
    Exit Sub
FuncErr:
    Dim msg As String
    Select Case Err.Number
        Case 6:
            msg = "This is an evil overflow. The number is too big!"
        Case 13:
            msg = "This is an evil type mismatch!"
        Case Else
            Err.Raise Err.Number, Err.Source, Err.Description
    End Select
        
    If msg <> "" Then
        MsgBox msg, vbCritical, "Trapped Error"
        End
    End If
 
End Sub

To assign distinct error handling behavior for different types of errors that could be raised, you need to use the ancient line label technique to trap an error, and then look-up the error number and use a Select Case statement to test for it. Moreover, a waterfall is in Visual Basic program control, even when using line labels. This means that program flow keeps moving from line to line within a procedure, even when you encounter a line label. Thus, when it comes to error trapping using line labels, if you forget to use an Exit Function or Exit Sub statement before the line label that denotes the beginning of the error code, your program will descend into the error handling area. More than once I’ve hosed myself by forgetting to exit a procedure before the error handling code, and I like to think I know what I am doing! It’s not an uncommon mistake. Current Visual Basic error handling is just not modern and versatile.

On the other hand, Java takes the modern approach using structured exception handling. In the OOP world, errors are called exceptions. Each of type of error is encapsulated into a class that is derived from a base Exception class. You trap exceptions using the try…catch structure. The following listing shows a Java try…catch structure, which ensures that the string value that is retrieved from a TextField is a numeral. If the value is not a numeral, the integer to which the value is to be assigned is given a value of –1. (This is very similar to using the Visual Basic IsNumeric() function in order to make sure that a value entered in a TextBox control is a number.)

{
    int j;

    try{
    Integer i = new Integer(textField2.getText());
    j = i.intValue();
}
catch(NumberFormatException e){
    j = -1;     
}

The benefit of structured exception handling is that all of the code is centralized around the area of the program that the exception will affect. There isn’t any jumping around lines of code as you see in VB. I am quite thrilled that VB 7 plans to support structured exception handling. I will not miss the line labels or the Exit Sub and Select Case statements.

  • + Share This
  • 🔖 Save To Your Account