Updated July 5, 2023
Introduction to Exception Handling in Vb.Net
The following article Exception Handling in VB.NET provides a detailed outline for handling exceptions in VB.Net. Welcome to the topic of exception handling in VB.NET. Let us learn what is exception handling is how the different exceptions occur and how do we handle these exceptions. Exceptions occur when we are running a program and an error is caught which leads to an exception which can be either a System Exception or Application Exception. We will learn more about this in the Exception Classes further. In this topic, we are going to learn about Exception Handling in VB.NET.
Keywords of Exception Handling in Vb.Net
Exception Handling in VB.NET is based on the use of four keywords like try, catch, finally and throws.
- Try: A Try Block is a block of code which generates different exceptions within the application. To handle these exceptions thrown in the try block we use catch block. Thus the try block is always followed by one or more catch blocks.
- Catch: A Catch Block is a block of code that contains the statements which handle the exceptions by using an exception handler on the statement where the issue occurred. The catch block is mainly the handling of exception by different Exception Classes defined in the library of Exceptions.
- Finally: A Finally Block is a block of code that contains statements that are mandatory statements to be executed irrespective of whether the exception is thrown or not thrown. Suppose I have to open a file, read the file, write the file and close the file. In the try block, statements are like open a file, read a file and write a file that may or may not throw an exception but the file needs to be closed before the program ends. Thus the final block has always statements like closing the files, closing the database connection, the closing of program, etc.
- Throw: When an issue occurs this statement throws an exception. This exception may be build in or user-defined exception.
Syntax:
The code for Try-Catch Block is as per the following
Try
Catch <Exception Name> As <Exception Type>
[Catch1 Statements]
Catch <Exception Name> As <Exception Type>
[Catch2 Statements]
Catch <Exception Name> As <Exception Type>
[Catch3 Statements]
Finally
[Finally Statements]
End Try
Here, there is more than one catch block displayed in the above syntax to catch a different type of exceptions raised by the try block. It may happen that more than one exception being thrown by the try block and only one catch block is available, then to catch all the exceptions we will need one or multiple catch statements. And thus we have written more than one catch statement in the syntax above.
Exception Classes in VB.NET
As already mentioned in the above statement. Exceptions are of different types and these are nothing but classes. The parent class from where these classes are inherited is System.Exception.
There are two Exceptions mainly
- System.SystemException
- System.ApplicationException
System.SystemException is a class for all built-in system exceptions or we can say that when running time error when occurred the system exception classes like DivideByZeroException, IndexOutOfRangeException, StackOverflowException is instantiated.
System.ApplicationException is a class that executes the exceptions defined by the programmer or developer withing the application. It throws a user-defined exception which is derived from this System.ApplicationException class.
Let us have a look at few VB.NET Exception Classes and its usage.
- System.IndexOutOfRangeException: This exception handles the errors caused by an array index which is out of range.s Suppose we have an array of 5 array lengths and we loop this 6 times the 6th time the program will through IndexOutOfRangeException.
- System.DivideByZeroException: This exception handles the errors caused when a number is divided by zero which is against the universal rule that a number being divided by zero will result in an abnormal termination of the program.
- System.FormatException: This exception handles the errors caused when the user inputs a character or a string when asked for a number or integer. say the user needs to enter 5 but inputs say ‘y’ then this exception will occur.
Examples of Exception Handling in VB.NET
These are a few exceptions explained. Lets now learn to handle these exceptions in VB.Net with example
Examples #1
Below is the program for DivideByZeroException and its output
Code
Module exceptionProg
Sub division(ByVal n1 As Integer, ByVal n2 As Integer)
Dim res As Integer
Try
res = n1 \ n2
Catch ex As DivideByZeroException
Console.WriteLine("Exception caught: {0}", ex)
Finally
Console.WriteLine("Result: {0}", res)
End Try
End Sub
Sub Main()
division(100, 0)
Console.ReadKey()
End Sub
End Module
Output
Example #2
Program for OverflowException
Module OverFlowProg
Sub division(ByVal n1 As Integer, ByVal n2 As Integer)
Dim res As Integer
Try
res = n1 \ n2
Catch ex As DivideByZeroException
Console.WriteLine("Exception caught: {0}", ex)
Finally
Console.WriteLine("Result: {0}", res)
End Try
End Sub
Sub Main()
division(25,777777777777)
Console.ReadKey()
End Sub
End Module
Output
Further, lets now create our own Customized Exceptions
In this program, we will check whether the number is even or odd,
Logic: The number is divided by 2 if the result is 0 then it is an even number else if the result is not 0 it is an odd number.
Code for the program is
Module EvenorOddNo
Public Class EvenorOddNo : Inherits ApplicationException
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
Public Class Divide
Dim AnyNo As Integer = 55
Dim No As Integer = 2
Sub showNumber()
If (AnyNo Mod No > 0) Then
Throw (New EvenorOddNo("The number is an odd number"))
Else
Console.WriteLine("The number is an even number : {0}", AnyNo)
End If
End Sub
End Class
Sub Main()
Dim obj As Divide = New Divide()
Try
obj.showNumber()
Catch ex As EvenorOddNo
Console.WriteLine("EvenorOddNo: {0}", ex.Message)
End Try
Console.ReadKey()
End Sub
End Module
Output
Similarly, if we enter the number as 100 (instead of 55) in the above program and execute, it will show the following error
Conclusion
Hopefully, you understood the topic, Exception Handling in VB.NET and will be able to grasp with the help of different examples mentioned here
Recommended Articles
This is a guide to Exception Handling in VB.NET. Here we discuss Exception Classes in VB.NET and sample code to handle the Exceptions. You may also have a look at the following articles to learn more –