Handling Exceptions

Handling Exceptions.

As mentioned earlier, there are two ways of handling errors that occur at run time in VB .NET—with structured and unstructured exception handling. What’s an exception? Exceptions are just runtime errors; in Visual Basic (unlike some other languages), the terms exception handling and error handling have become inter-changeable. Exceptions occur when a program is running (as opposed to syntax errors, which will prevent VB .NET from running your program at all). You can trap such exceptions and recover from them, rather than letting them bring your program to an inglorious end.

Unstructured Exception Handling

The old error-handling mechanism in VB6 and before is now called unstructured exception handling, and it revolves around the On Error Goto statement. You use this statement to tell VB .NET where to transfer control to in case there’s been an exception, as in this case, where I’m telling Visual Basic to jump to the label “Handler” if there’s been an exception. You create labels in your code with the label name followed by a colon, and the exception-handling code will follow that label (note that I’ve added an Exit Sub statement to make sure the code in the exception handler is not executed by mistake as part of normal program execution):

Module Module1    Sub Main()        On Error Goto Handler        ⋮        Exit SubHandler:        ⋮    End SubEnd Module

Now I can execute some code that may cause an exception, as here, where the code performs a division by zero, which causes an exception. When the exception occurs, control will jump to the exception handler, where I’ll display a message and then use the Resume Next statement to transfer control back to the statement immediately after the statement that caused the exception:

Module Module1    Sub Main()        Dim int1 = 0, int2 = 1, int3 As Integer        On Error Goto Handler        int3 = int2 / int1        System.Console.WriteLine("The answer is {0}", int3)Handler:        System.Console.WriteLine("Divide by zero error")        Resume Next    End SubEnd Module

When you run this code, you see this message:

Divide by zero error

Structured Exception Handling

Visual Basic also supports structured exception handling. In particular, Visual Basic uses an enhanced version of the Try…Catch…Finally syntax already supported by other languages, such as Java. Here’s an example that follows our previous example handling a division by zero exception; I start by creating a Try block—you put the exception-prone code in the Try section and the exception-handling code in the Catch section:

Module Module1    Sub Main()        Try        ⋮        Catch e As Exception        ⋮        End Try    End SubEnd Module

Note the syntax of the Catch statement, which catches an Exception object that I’m naming e. When the code in the Try block causes an exception, I can use the e.ToString method to display a message:

Module Module1    Sub Main()        Dim int1 = 0, int2 = 1, int3 As Integer        Try            int3 = int2 / int1            System.Console.WriteLine("The answer is {0}", int3)        Catch e As Exception            System.Console.WriteLine(e.ToString)        End Try    End SubEnd Module

Here’s what you see when you run this code:

System.OverflowException: Exception of type System.OverflowException wasthrown.   at Microsoft.VisualBasic.Helpers.IntegerType.FromObject(Object Value)   at ConsoleHello.Module1.Main() in   C:vbnetConsoleHelloModule1.vb:line 5

Besides using the e.ToString method, you can also use the e.message field, which contains this message:

Exception of type System.OverflowException was thrown.

And now it’s time to turn to the Immediate Solutions section to see the details on creating procedures, setting scope, and handling exceptions.

Leave Your Response