Visual Basic Exception

In visual basic, Exception is an unexpected event or an error which may occur during the execution of a program and it will provide necessary information about the error which occurred in our application.


Generally, the exceptions are thrown by .NET framework CLR (Common Language Runtime) or by the code in our program. Following is the example which will throw an exception when we execute it in visual basic.


Module Module1

    Sub Main(ByVal args As String())

        Dim name As String = Nothing

        ' Null Reference Exception Error

        If name.Length > 0 Then

            Console.WriteLine("Name: " & name)

        End If

    End Sub

End Module

If you observe the example, we assigned a Nothing value to the name variable and trying to validate the length of string variable.


When we execute the above visual basic program, we will get an exception like as shown below because the variable (name) value is Nothing or null.


Visual Basic NullReferenceException Example Result


Visual basic has provided necessary built-in exception classes to handle all the exceptions in our applications and all exception classes are derived from the base class called Exception. The Exception class will identify the type of exception and provide the properties that have details about the exception.


In visual basic, the Exception class further classified into two other classes called SystemException and ApplicationException to handle the exceptions. Generally, the SystemException is a base class for all CLR generated errors whereas the ApplicationException is a base class for all application-related exceptions.


The following diagram will illustrate more details about the exception classes in visual basic.


Visual Basic (VB) System & Application Exceptions Hierarchy Diagram


If you observe the above diagram, the SystemException is a base class for all CLR exceptions and these will occur during the execution of the program. The ApplicationException is a base class for application-related exceptions and we can derive this class to create our own exception classes. 

Visual Basic Exception Classes

Following table lists some of the important exception classes available in visual basic.


AccessViolationException This exception will raise when there is an attempt to read or write protected memory.
ArgumentException This exception will be thrown when one of the arguments provided to a method is not valid.
ArgumentNullException This exception will occur when null argument is passed to a method that does not accept it as a valid argument.
ArgumentOutOfRangeException This exception will occur when the value of an argument is outside the allowable range of values.
ArithmeticException This exception will occur for an errors in arithmetic, casting, or conversion operation.
DivideByZeroException This exception will occur when we try to divide an integral or decimal value by zero.
FormatException This will occur when the format of an argument is invalid.
IndexOutOfRangeException This will occur when we try to access an element of an array or collection with an index that is outside of its bounds.
InvalidCastException This exception occur for invalid casting or explicit conversion.
NullReferenceException This exception will occur when we try to reference an object which is of NULL type.
OutOfMemoryException This will occur when the program is not having enough memory to execute the code.
OverflowException This will occur when arithmetic, casting, or conversion operation results in an overflow.
StackOverflowException This exception will occur when the execution stack overflows.
TimeoutException This will occur when the time allotted for a process or operation has expired.

As we discussed, every exception class will derive from the base Exception class and provide the following important properties to get the details of an exception.


Data This property will hold arbitrary data in key-value pairs.
HelpLink It will hold a help file URL that provides detailed information about the cause of an exception.
InnerException This property will provide an information about the series of exceptions that might have occurred.
Message It will provide the details about the cause of an exception.
Source It will set or get the name of an application or object that causes the error.
StackTrace It provide a stack trace to determine where an error occurred.

Generally, in visual basic we can handle all the exceptions by using Try-Catch blocks. In the next chapter, we will learn how to handle exceptions in vb with examples.