Structured Exception Handling

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 2.   New Features in Visual Basic .NET


In earlier versions of Visual Basic, error handling was done with the help of OnError.. GoTo statements. However this is not a structured way to handle errors. Visual Basic .NET offers structured error handling. It has introduced the try , catch , and finally blocks.

The code, which can potentially throw an error, is put in a try block. Errors are simply objects of type Exception . Every try block should have a corresponding catch block. If an exception arises in the try block, the code throws an exception . This exception is caught and handled by the catch block. The catch block can handle the exception , or it can rethrow the exception for some higher-level code to handle it.

Some resource cleanup such as closing database connections and closing files must always be executed even though exceptions are thrown by the code. Visual Basic .NET uses a finally block with the try and catch blocks. Code written in the finally block is always executed whether an exception is thrown by the code or not. Therefore, code to clean up the resources can be put in the finally block. This ensures that resources would be cleaned up whether or not exceptions are thrown by the code.

The following code example is found in the folder ExceptionApplication for this chapter. This code gives an example of an exception being thrown by a try block and correspondingly handled by the catch block.

 graphics/icon01.gif Imports System  Module MainModule     Sub Main()        Try           Dim iNumber, IResult As Integer           iNumber = 10           IResult = iNumber / 0        Catch           Console.WriteLine("Exception thrown has " &_            "been caught")        End Try        Console.ReadLine()     End Sub  End Module 

In the example, zero is used to divide the integer variable iNumber . This results in an exception . The code contained in the catch block is executed, writing the string "Exception thrown has been caught" to the console.

When an exception is raised, the code tries to go to a catch block that handles the specific exception. If the specific exception is not caught and the generic exception is also not caught, control passes to the calling program to check if the exception-handling code is placed in the call stack. Thus Visual Basic .NET moves up the call stack until it finds the appropriate exception-handling code. If the exception is not handled, the exception is raised at the application level and the program terminates.

The preceding example provides a generic catch block. It is possible to catch specific exceptions such as FileNotFoundException and NullReferenceException . Visual Basic .NET also allows developers to create their own application-specific exception classes. These classes have to inherit from the generic Exception class.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

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