Exception Handling

Using VBA, you typically handle errors by using an On Error statement that specifies the location of the error handling code. When an error is encountered, execution moves to the previous On Error statement in the subroutine, which then moves execution to the location specified. The On Error statement can specify three things:

• The code execution should move to a particular line or label (On Error GoTo xErrorHandler).

• The code execution should move to the next statement in the code where the error occurred (On Error Resume Next).

• The error handling should be disabled within the procedure (On Error GoTo 0).

Visual Basic 2005 supports the On Error statement, but you should consider using structured error handling. Structured error handling in Visual Basic 2005 translates to the Try Catch Finally (Try Catch) statements (see Listing 4.14). These statements enable you to catch errors that are thrown at run time and handle the errors gracefully so that the application doesn't just crash unexpectedly. You can write code to handle specific exceptions that you know might occur in your code, or you can catch unspecified exceptions in a general Catch block.

Listing 4.14. Try Catch statement Try

' Code that might cause an exception.

Catch ex As Exception

' Code to handle the exception.


' Additional code to run whether or not exception occurs. End Try

Visual Basic 2005 has the Try Catch statement available as a code snippet. Three statements are available, and each snippet has a corresponding shortcut that you can expand by typing the shortcut name and then pressing the TAB key.

• Try Catch EndTry, which has the shortcut TryC

• Try Catch Finally EndTry, which has the shortcut TryCF

• Try Finally EndTry, which has the shortcut TryF

To add a Try Catch statement using a code snippet shortcut, type TryCF in the Code Editor, and then press the TAB key. Notice that the variable ApplicationException in the Catch statement is automatically highlighted. You should change this variable to the type of exception that you expect. For example, you can change it to a NullReferenceException if it is possible that a variable has not yet been assigned a value, as shown in Listing 4.15. You can have more than one Catch block to handle different types of exceptions.

You add the code that might cause an error to the Try block. If an error occurs, execution moves to the Catch block, which should contain the code that handles the error. Code execution then moves to the first statement after the End Try statement. If the optional Finally statement is present before the End Try statement, the code execution always moves to the Finally block. Code within a Finally block runs after the errors are handled and runs even if an error is not encountered. Often, developers add cleanup code, such as closing a database connection, to the Finally block.

Listing 4.15. Catching a NullReferenceException Dim myString As Object = Nothing


Catch ex As NullReferenceException

MsgBox(ex.Message) End Try

If code in a method encounters an error and if a Catch block for the type of exception thrown cannot be located, the exception is passed up to the calling method. This continues until the top of the call stack is reached. If a Catch block is still not found, the default exception handler runs. The Message property of the exception contains information about the cause of the error, and you can display this information to end users in a message box. You can even provide a link to a Help topic that contains additional information.

If you plan to upgrade existing VBA code to Visual Basic 2005, keep in mind that you cannot combine the VBA-style error handling with structured error handling within the same methods. Doing so will cause a compiler error, as shown in Figure 4.6.

¡Method cannot contain both a 'Try' statement and an 'On Error' or 'Resume' statement.|

MessageBox . Show (rnyString. ToString () ) Catch ex As NullReferenceException

Figure 4.6. Error when using a Try statement with an On Error statement

0 0

Post a comment