July 22, 2018
Hot Topics:

The Book of Visual Studio .NET - Structured Exception Handling in VB

  • January 22, 2003
  • By Developer.com Staff
  • Send Email »
  • More Articles »

The following is the second of three parts of a chapter on Visual Basic .NET from The Book of Visual Studio .NET, ISBN 1-886411-69-7 from No Starch Press written by Robert B. Dunaway. The first part covered some of the differences between VB .NET from the previous version. This part will present structured exception handling in Visual Basic .NET. The third part will present OOP concepts and VB .NET.

A   V I S U A L   B A S I C . N E T
C R A S H   C O U R S E

Structured Exception Handling

One of the more complex and important aspects of application development iserror handling. Errors that are not handled can have devastating effects on thesuccess of any application because, in most cases, the application will not beable to recover or shut down gracefully. Among the challenges is the lack ofcomprehensive error handling across languages and platforms.To complicate matters further, handling errors is only t he tip of theproverbial iceberg. As important as error handling is, an application must beable to handle unacceptable application level events or exceptions that are notnecessarily system generated error. Now, the term "error handling" is nolonger sufficient.

Exception handling deals with any system or application generated error,what we now refer to as an exception. This ability to handle all exceptions, bothsystem and application generated, goes a long way toward giving the user a stableapplication and a better overall user experience.

The CLR is a language independent means for exception handling thatplaces all raised or thrown errors into an exception object that can be manipulatedlike any other object. In addition, the exception object can be created andused, programmatically, anywhere within an application.

The CLR implements exception handling with the Try/Catch/Throwmodel. This model of exception handling, while foreign to Visual Basic programmers,is well known by C++ programmers. This is a structured exceptionhandling model that is time tested and proven as a solid means for handling ordealing with thrown exceptions.

Throwing an exception is similar to raising an error in Visual Basic. Thesimplest exception structure is as follows:

Public Sub MySub()Try'Some codeCatch'Deals with any exception that may occur.End TryEnd Sub

In its most basic form, all application and exception code goes into what iscalled a Try block. In this example, the Try block is the area between the Tryand the End Try statements.

The Try block can be divided into three clauses. The area between the Tryand Catch statements is where you place your applications code. The areabetween the Catch and, in this case, the End Try statements are where yourexception handling code resides. Another section, defined as the finally clause,is available in the last clause where the Try blocks code executes. Our simpleexception handling example does not use the finally clause as it is not requiredwhen a Catch statement is available.

Exception Handling Rules

A set of rules governs how the Try block can be used. But before we have a lookat these rules, let's take a quick look at some of the classes that support exceptionhandling.

Table 7-3 lists several common exception classes you can look forward tousing. Exception classes are thrown when a related exception is thrown; whenlooking for exceptions look for the following:

Table 7-3: Exception Classes
Exception Class Reason Exception Class Is Thrown

System.AppDomainUnloadedException Thrown when attempting to use an unloadedapplication domain.

System.ApplicationException Thrown when a non-fatal application error hasoccurred.

System.ArgumentException Thrown when an argument passed is not valid.

System.ArgumentNullException Thrown when a null is passed as a method parameterthat does not accept null values.

System.ArgumentOutOfRangeException Thrown when a passed value is outside the range of amethods parameter.

System.ArithmeticException Thrown when an error occurs while performingarithmetic and conversion operations.

System.ArrayTypeMismatchException Thrown when adding a value of the incorrect data typeto an array.

System.DivideByZeroException Thrown whenever a value is divided by zero.
System.DllNotFoundException Thrown when a DLL referenced as imported is not available.

System.IndexOutOfRangeException Thrown when trying to access an invalid index in an array.

System.InvalidCaseException Thrown when an invalid conversion attempt is made.

System.NullReferenceException Thrown when attempting to dereference a null object reference.

System.OutOfMemoryException Thrown when memory is not available to perform the specified task.

System.OverflowException Thrown when an operation overflow occurs.

System.RankException Thrown when an array with the wrong number of dimensions is passed to a methods parameter.

System.SystemException Is the base class for all exception classes in the System namespace.

System.Data.ConstraintException Thrown when a constraint is violated.

System.Data.DataException Thrown when an ADO.NET component generates an error.

System.Data.DBConcurrencyException Thrown when the number of rows affected in an update procedure is zero.

System.Data. DeletedRowInaccessibleException Thrown when attempting to perform data manipulation operations on a data row that has been deleted.

System.Data. InvalidConstraintException Thrown when a data relationship is violated.

System.Data. NoNullAllowedException Thrown when inserting a null value where one is not accepted.

System.IO. DirectoryNotFoundException Thrown when a specified directory cannot be found.

System.IO.FileLoadException Thrown when a file cannot be loaded.

System.IO.IOException Thrown when an I/O error occurs.

System.IO.PathToLongException Thrown when a path or file name are too long.

System.Runtime.Remoting. Thrown when an error occurs during a remote RemotingException operation.

System.Runtime.Remoting. RemotingTimeoutException Thrown when the response of a server or client exceed a predefined interval.

System.Runtime.Remoting.ServerException Thrown when an error occurs while working with a remote component that is an unmanaged application incapable of throwing an exception.

System.Runtime.Serialization.SerializationException Thrown when an error occurs during the serialization or deserialization of a component.

System.Web.HttpException Allow an http exception to be thrown.

System.XML.XmlException Provides exception information about the last XML exception.

Page 1 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date