Structured Exception Handling in VB.NET Los Angeles CA

Learn to handle exceptions properly in VB.NET by utilizing Structured Exception Handling.

Local Companies

Moyea Software
92295612365
Hot building, ring street
LA, CA
Interneer Inc.
8005586832 x85
6101 W. Centinela Ave.
Culver City, CA
Cornerstone Concepts Inc
818-247-3909
600 W Broadway
Glendale, CA
Greene Computer Corporation
(818) 956-4961
200 S. Louise Street
Glendale, CA
Corticalx Inc Software Solutions & Technology
818-500-0881
425 E Colorado St
Glendale, CA
Alphatier Systems
818-409-8920
517 Griswold St
Glendale, CA
TimeTECH - Customizable Time and Attendance / Workforce Management Solutions
905-677-7009
7420 Airport Rd 203
Mississauga, CA
Hutchinson & Bloodgood, LLP
(818) 637-5000
101 N. Brand Blvd. #1600
Glendale, CA
Telsoft Solutions
818-545-8680
100 N Brand Blvd
Glendale, CA
Abraxas Technologies Inc
818-502-9100
450 N Brand Blvd
Glendale, CA

provided by: 
Originally published at Internet.com


Introduction

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.

Conclusion

.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.

Author: Irina Medvinskaya

Read article at Internet.com site

Featured Local Company

Moyea Software

92295612365
Hot building, ring street
LA, CA

Related Local Events
Automation Technology Expo West (ATX West)
Dates: 2/9/2010 - 2/11/2010
Location: Anaheim Convention Center
Anaheim, CA
View Details

SOLAR POWER - Exhibition and Conference
Dates: 10/12/2010 - 10/14/2010
Location: Los Angeles Convention & Exhibition Center
Los Angeles, CA
View Details

REAL-TIME & EMBEDDED COMPUTING CONFERENCE - LONG BEACH 2009
Dates: 10/1/2009 - 10/1/2009
Location: Marriott Long Beach
Long Beach, CA
View Details

2009 IEEE Petroleum and Chemical Industry Technical Conference (PCIC 2009)
Dates: 9/14/2009 - 9/16/2009
Location:
Anaheim, CA
View Details

Medical Design & Manufacturing - Trade
Dates: 6/9/2009 - 6/11/2009
Location: CANON COMMUNICATIONS LLC
Los Angeles, CA
View Details