For years, the weak part of most of the languages supported by Microsoft has been the error handling capability. In most cases this error handling was a variant of 2 statements:

On Error Goto; and

On Error Resume Next

Which resulted in branching to an End statement, or continuing on as if nothing had happened.

Taking a page from the Java model, Microsoft, in order to achieve cross platform uniformity with all of the .Net languages, has adopted the

Try..Catch..Finally..End Try method of handling errors. This allows an error to be handled in the routine in which it occurs. If the exception is not caught, it is referred to the calling procedure, and so on and so on. If no exception is caught by the end of the calling chain, the user is notified by a dialog box.

Exception objects have 4 properties in common. The Message property is the text which describes the error. It is similar to the old Err.description. StackTrace returns a string which traces the path from where the exception was originally thrown. The Source property return s the name of the routine where the exception was thrown. This is analogous to Err.Source. HelpLink returns the URL or URN to the helpfile associated with the error.

Some of the more common exceptions are listed in the following table:


Thrown when attempting to use an unloaded application domain.


Thrown when a non-fatal application error has occurred


Thrown when an argument passed is not valid.


Thrown when a null is passed as a method parameter that does not accept null values.


Thrown when a passed value is outside the range of a methods parameter.


Thrown when an error occurs while performing arithmetic and conversion operations.


Thrown when adding a value of the incorrect data type to an array.


Thrown whenever a value is divided by zero.


Thrown when a DLL referenced as imported is not available


Thrown when trying to access an invalid index in an array


Thrown when an invalid conversion attempt is made.


Thrown when attempting to dereference a null object reference


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


Thrown when an operation overflow occurs


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


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


Thrown when a constraint is violated.


Thrown when an ADO.NET component generates an error


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.


Thrown when a file cannot be loaded.


Thrown when an I/O error occurs


Thrown when a path or file name are too long.


Thrown when an error occurs during a remote RemotingException operation


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


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


Thrown when an error occurs during the serialization or deserialization of a component.


Allow an http exception to be thrown


Provides exception information about the last XML exception.


Immediately following the TRY command, the code to be executed is placed. If an error is detected, control is passed immediately to the CATCH commands. The CATCH commands should be listed with the most restrictive listed first. There are in most cases multiple CATCH blocks to accommodate different types of errors and different resolutions for each. You can use s WHEN with a CATCH line to additionally specify conditions for that CATCH block to be executed. A TRY block can be exited at any time upon encountering an EXIT TRY command

Dim a,b as Integer




Catch exn as DividebyZeroException

Console.WriteLine(“You tried to divide by zero”)

Catch exn as OverflowException ‘never executed

Console.WriteLine(“Number too big”)




End Try



The FINALLY clause is optional, and used to clean up any code or partially executed block of code which may have been interrupted. The code between FINALLY and ENDTRY is guaranteed to be executed whether or not an exception was thrown.

You can also THROW an exception (create your own error to trigger) by using the THROW keyword, and using the Namespace and Exception as a method, as in:

THROW New System.DividebyZeroException()

Try to use catching exception as sparingly as possible because they add overhead to the project. On the other hand, it is important to trap errors because in so doing you relieve the user of the burden of figuring out what went wrong. In addition, it is often a good idea to keep a database of errors to monitor the performance of the application over time.



    * Information on System.IndexOutOfRangeException Class

This resource provides information on System.IndexOutOfRangeException Class.

    * Information on System.ArgumentException Class

This resource provides information on System.ArgumentException Class.