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.
Sub Main(ByVal args As String())
Dim name As String = Nothing
' Null Reference Exception Error
If name.Length > 0 Then
Console.WriteLine("Name: " & name)
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 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 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.
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.
Following table lists some of the important exception classes available in visual basic.
|This exception will raise when there is an attempt to read or write protected memory.
|This exception will be thrown when one of the arguments provided to a method is not valid.
|This exception will occur when null argument is passed to a method that does not accept it as a valid argument.
|This exception will occur when the value of an argument is outside the allowable range of values.
|This exception will occur for an errors in arithmetic, casting, or conversion operation.
|This exception will occur when we try to divide an integral or decimal value by zero.
|This will occur when the format of an argument is invalid.
|This will occur when we try to access an element of an array or collection with an index that is outside of its bounds.
|This exception occur for invalid casting or explicit conversion.
|This exception will occur when we try to reference an object which is of NULL type.
|This will occur when the program is not having enough memory to execute the code.
|This will occur when arithmetic, casting, or conversion operation results in an overflow.
|This exception will occur when the execution stack overflows.
|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.
|This property will hold arbitrary data in key-value pairs.
|It will hold a help file URL that provides detailed information about the cause of an exception.
|This property will provide an information about the series of exceptions that might have occurred.
|It will provide the details about the cause of an exception.
|It will set or get the name of an application or object that causes the error.
|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.