Unstructured Exception Handling

 <  Day Day Up  >  

In addition to structured exception handling, Visual Basic .NET supports another style of exception handling that is more compatible with the style of exception handling used in previous versions. This style is called unstructured exception handling because it does not explicitly specify which statements a particular handler might cover and is a much looser way of handling exceptions.

Style

Structured exception handling involves less overhead and is more precise than unstructured exception handling. Although there are no major technical disadvantages to using unstructured exception handling, the Try ... Catch style of error handling is encouraged over On Error .


In a method that uses unstructured exception handling, one or more On Error statements establish the behavior when any exception occurs (it isn't possible to catch only certain types of exceptions using unstructured exception handling). An On Error statement can be followed by a GoTo statement that specifies where execution should be transferred to when an exception is caught. In the following example, the On Error statement specifies that execution should jump to the HandleError label when an exception occurs.

 Dim x, y As Integer On Error Goto HandleError x = CInt(Console.ReadLine()) y = CInt(Console.ReadLine()) Console.WriteLine(x \ y) Return HandleError: If TypeOf Err.GetException() Is OverflowException Then   Console.WriteLine("Overflow") Else If TypeOf Err.GetException() Is DivideByZeroException Then   Console.WriteLine("Divide by zero") Else   Console.WriteLine(Err.GetException().Message) End If 

Unlike a Try statement, which applies only to the statements contained within it, an On Error statement establishes the exception handling behavior for the entire method. This is done every time the point of execution reaches the particular On Error statement. For example, by the time execution reaches the end of the following set of statements, the current exception behavior will be to go to the First label, because it was the last On Error statement to be executed.

 Dim x, y As Integer On Error Goto First y = 0 x = 5 If y > 0 Then   On Error Goto Second End If x = x \ y Return First: Console.WriteLine("First handler") Return Second: Console.WriteLine("Second handler") Return 

When execution enters a method that contains On Error statements, no handler is initially set up, and exceptions will not be caught in the method. Once an exception handler has been set, the statement On Error Goto 0 will set the current exception handler back to nothing. Once an exception has been caught, the exception is available in the Err property in the Microsoft.VisualBasic.Information standard module. The Err property returns an instance of the Microsoft.VisualBasic.ErrObject class. (For more information on Err and ErrObject , see Appendix A.)

 Dim x, y As Integer On Error Goto Handler y = 0 x = 5 x = x \ y Return Handler: Console.WriteLine("Exception: " & Err.GetException().Message) Return 

The exception is stored in the property until the next exception occurs or until the method exits. The exception can be explicitly cleared by the statement On Error Goto -1 .

Resume and Resume Next

One feature that unstructured error handling supports and that structured exception handling does not is resuming from exceptions. After an exception has occurred, the Resume or Resume Next statement will return execution to the statement that caused the exception or the statement following it, respectively. In the following example, when the divide-by-zero exception is thrown, the code will change the value of y to 1 and then reexecute the division.

 Dim x, y As Integer On Error Goto Handler y = 0 x = 5 Return x \ y Handler: y = 1 Resume 

It is important to note that Resume puts the execution point back at the beginning of the statement that caused the exception, not the exact point where an exception occurred. In the following example, if the method invocation b() causes an exception, the Resume statement will cause the method a() to be invoked again.

 Dim x As Integer On Error Goto Handler x = a() + b() Return Handler: Resume 

The On Error statement can also be used in conjunction with the Resume Next statement. (The statement On Error Resume is not allowed, because it would be likely to result in an infinite loop of exceptions.) In general, it is extremely bad programming practice to use the On Error Resume Next statement except in very limited situations. Exceptions are thrown to indicate error conditions, and reflexively ignoring all exceptions could allow potentially serious bugs . For example:

 On Error Resume Next Dim x As FileStream = File.Open("output.txt", FileMode.Create) x.WriteByte(10) x.WriteByte(20) x.Close() 

In this example, if the file fails to open for some reason, all the method invocations that follow will fail as well. But the On Error Resume Next statement causes no exceptions to be raised, and the code will run with no complaint, even though its intended purpose has not been fulfilled.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

Similar book on Amazon

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