Structured versus Classic Error Handling


The newer structured error-handling approach provided by the Try statement has several advantages over classic error handling. First, classic error handling doesn’t make it immediately obvious whether a piece of code is protected by an error handler. To determine whether a statement is protected, you must look back through the code until you find an On Error statement. If you come to a labeled line, you also must track down any places where a GoTo or a Resume line statement could jump to that line and see what error handler might be installed at the time.

Classic error handling also doesn’t make it obvious whether the code is running in error-handler mode. In some cases, it is impossible to tell until runtime. The following code uses an On Error GoTo statement to protect itself and then initializes an integer from a value that the user entered in a text box. If the user enters a valid integer, the code works normally and keeps running in normal (not error-handling) mode. If the user enters a value that is not a valid integer, the program jumps to the label BadFormat and enters error-handling mode. There’s no way to tell before runtime whether the program will be in error-handling mode when it reaches the following comment.

      Dim i As Integer     On Error GoTo BadFormat     i = CInt(txtNumber.Text) BadFormat:     ' Are we in error-handling mode here?     ...  

Finally, you cannot nest classic error-handling code. If you must perform a risky action in an error handler, you must place the code in a separate subroutine that contains its own error-handling code to protect itself.

Structured error handling addresses these shortcomings. By looking at the enclosing Try or Catch block, you can easily tell whether a line of code is protected (inside the Try block) or part of an error handler (in the Catch block).

You can even nest Try statements, as shown in the following code. The program tries to initialize an integer from a value that the user entered in a text box. If the user enters an invalid value, the code moves into the first Catch block. There it tries to set the value of the integer using a calculation. If that calculation fails (for example, if j is 0), the next Catch block sets the variable to a default value.

  ' Get the user's Try     i = Integer.Parse(txtNumber.Text) Catch ex As Exception     ' The user's     ' Calculate a different value.     Try         i = 1 \ j     Catch ex2 As Exception         ' The calculated value is no good.         ' Use a default value.         i = 3     End Try End Try  

Finally, the Try block doesn’t have a bewildering error-handling mode. The potential for confusion there alone is probably worth using structured error handling.

One of the few advantages to classic error handling is that it is easier to ignore errors by using the On Error Resume Next statement. The following code uses classic error handling to execute three subroutines and ignore any errors they produce:

  On Error Resume Next DoSomething() DoSomethingElse() DoSomethingMore() ... 

The following version shows the same code using structured error handling. This version is quite a bit more verbose.

  Try     DoSomething() Catch End Try Try     DoSomethingElse() Catch End Try Try     DoSomethingMore() Catch End Try ...  




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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