Structured Exception Handling in VB.NET
VB.NET improved a number of features compared to the legacy VB language. One of the most notable changes is the introduction of structured exception handling. Even though VB.NET still supports the On Error Goto type of error handling, it's not a good idea to use it. Instead, you should utilize the full power of the structured exception handling available in VB.NET.
This article will discuss the basics of structured error handling with VB.NET and look at examples of using exception handling in your applications.
The Importance of Proper Error Handling
Any application requires appropriate exception handling. But, the importance of well organized and thought-out exception handling has often been overlooked by developers due to strict deadlines, shared responsibilities, and conflicting priorities. This often results in user inconvenience and frustration and forces a lot of code rework.
Structured Exception Handling
VB.NET utilizes the .NET Framework's standard mechanism for error reporting, called Structured Exception Handling; it relies on exceptions to report errors that arise in applications. Exceptions are classes that trap the error information. To utilize .NET's Structured Exception Handling mechanisms properly, developers need to write smart code that watches out for exceptions and implement code to deal with these exceptions.
Structured exception handling provides the following components in the code:
- Try section: The block of code that may result in an exception and always gets executed
- Catch section: The block of code that attempts to act on an exception and is only executed when an exception takes place
- Finally section: The block of code intended to perform any kind of clean up operation and always gets executed
The Exception Class
Each exception class in .NET is derived from a System.Exception class. The most often used members of the Exception class are listed below:
- Message: Specifies details of an error
- Source: Name of the object or application that caused the exception
- TargetSite: Name of the method that threw the exception
The Try...Catch Block
The purpose of the Try...Catch block is to allow catching errors and specifying a resolution for them. The sample code looks like this:
Try 'Code to be executed Catch 'Error resolution code End Catch
Use the Try section to write the code that should be executed and the Catch section to catch and act on any errors that may have been generated while executing the code in the Try section. The protected code appearing in the Try section always gets executed; however, the code in the Catch section is executed only if an error occurs. The Try section of the code always gets executed; however, the Catch section of the code will be executed only if an error occurred.
The Try...Catch...Finally Block
The purpose of the Try...Catch...Finally block is to allow executing the protected code under the Try section, acting on any errors that may arise in the Catch block, and following up with the cleanup code in the Finally block. Code under the Finally block will be executed regardless of whether an error occurred in the Try code block or not. This provides a very convenient way of ensuring that allocated resources are being cleaned and performing any kind of functionality that needs to take place regardless of the error handling details. The sample code looks like this:
Try 'Code to be executed Catch 'Error resolution code Finally 'Cleanup code End Catch
The Try and Finally sections of the code always get executed. However, the Catch section of the code will be executed only if an error occurred.
Catching All Exceptions vs. Specific Classes of Exceptions
The structured exception handling in .NET is flexible and allows catching a specific type of an exception or any exception, depending on how you utilize it.
Example: Catching any exception that may occur
Try Dim i As Integer = 0 Dim iresult As Integer iresult = 1 / i Catch ex As Exception MessageBox.Show(ex.ToString()) Finally MessageBox.Show("finally block executed") End Try
How this works
In the code example above, you purposefully create a run-time error to demonstrate catching any exception. You catch any error and respond to it regardless of what kind of error occurred. The error takes place in the Try code block, so when the exception is raised it follows to the Catch code block and then to the Finally code block. You catch the exception by declaring a variable, ex, of the Exception type.
Example: Catching a specific Exception
Try Dim i As Integer = 0 Dim iresult As Integer iresult = 1 / i Catch ex As OverflowException MessageBox.Show(ex.ToString()) Finally MessageBox.Show("finally block executed") End Try
How this works
The second code example causes the same error because it attempts to perform division by zero; this results in an overflow. However, in this case, you are only interested in catching this type of exception and therefore specify that the ex variable should be of the type OverflowException. The result of running the code in the second example is exactly the same as in the first example because in both cases same error is caught; however, the second example would have not caught an exception of a different type (not of the overflow type). And as before, the code in the Finally block is executed either way.
.NET's Exception Handling allows a lot of flexibility and should be utilized wisely. Error handling in general should not be an afterthought in your applications but rather the framework under which every functionality is built to provide stable and solid applications for your users. Handle the exception handling in your applications with care. For more information, go to the Microsoft web site.
About the Author
Irina Medvinskaya has been involved in technology since 1996. Throughout her career, she as developed many client/server and web applications, mainly for financial services companies. She works as a Development Manager at Citigroup.