Visual Basic’s error handling is its own special WTF in itself. For those that haven’t had to suffer through it, you can set the error-handling mode with a special On Error statement. For example, On Error Resume Next, is a delightful statement that tells Visual Basic to simply ignore errors, and continue execution. A good programmer will know to check errors with conditional statements.

More common, is the On Error Goto ErrHandler approach. As the code implies, when an error occurs, this simply executes a goto to jump to a specific label, with all of the fun that goto statements normally include.

There are a few global variables that VB manages for you, so that you can find out what error occurred. Err contains the last error code that was raised. Erl contains the line of VB code that triggered the error, which is useful for printing out meaningful error messages.

Ryan inherited an application that used yet another VB WTF- Office Automation- to script behaviors in Microsoft Word. After he fixed one minor bug in the application by adding a single line of code, the application stopped working completely. It was difficult to debug, because there were a number of operations it performed that were likely to fail on the first few attempts, and so the error handler had retry logic built into it. Upon investigation, he found that it wasn’t ever retrying, and just insta-failing and quitting Microsoft Word.

See if you can spot why:

        loopctr = loopctr + 1
        If (Err=213 And (Erl = 49 Or Erl = 46 Or Erl = 44 Or Erl = 224 Or Erl = 39) ) Then
                If loopctr <= 100 Then
                        If (Err=213 And (Erl = 49 Or Erl = 46 Or Erl = 44) ) Then
                                Resume Again
                                If Erl = 39 Then
                                        Resume again3
                                        Resume again2
                                End If
                        End If
                End If
        End If
        Msgbox ("Error " & Err & " on line " & Erl & " - " & Error & " in CreateDocument")
        If Isobject(MSWORD) Then
                MSWord.Quit( wdDoNotSaveChanges )
        End If

You guessed right- Ryan’s predecessor, the Erl-king, used line numbers as magic numbers to determine what the exact source of the error was. Once Ryan added a line or two of code, the numbers were all off.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!