Exception Handling
Exception Handling
Exceptions provide a way to transfer control from one part of a program to another. VB.Net
exception handling is built upon four keywords - Try, Catch, Finally and Throw.
Try − A Try block identifies a block of code for which particular exceptions will be activated.
It's followed by one or more Catch blocks.
Finally − The Finally block is used to execute a given set of statements, whether an
exception is thrown or not thrown. For example, if you open a file, it must be closed
whether an exception is raised or not.
Throw − A program throws an exception when a problem shows up. This is done using a
Throw keyword.
Syntax
Assuming a block will raise an exception, a method catches an exception using a combination of
the Try and Catch keywords. A Try/Catch block is placed around the code that might generate
an exception. Code within a Try/Catch block is referred to as protected code, and the syntax for
using Try/Catch looks like the following −
Try
[ tryStatements ]
[ Exit Try ]
[ Catch [ exception [ As type ] ] [ When expression ]
[ catchStatements ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
https://www.tutorialspoint.com/vb.net/vb.net_exception_handling.htm 1/5
6/16/23, 3:57 AM VB.Net - Exception Handling
[ finallyStatements ] ]
End Try
You can list down multiple catch statements to catch different type of exceptions in case your
try block raises more than one exception in different situations.
The System.SystemException class is the base class for all predefined system exception.
The following table provides some of the predefined exception classes derived from the
Sytem.SystemException class −
Handling Exceptions
VB.Net provides a structured solution to the exception handling problems in the form of try and
catch blocks. Using these blocks the core program statements are separated from the error-
handling statements.
https://www.tutorialspoint.com/vb.net/vb.net_exception_handling.htm 2/5
6/16/23, 3:57 AM VB.Net - Exception Handling
These error handling blocks are implemented using the Try, Catch and Finally keywords.
Following is an example of throwing an exception when dividing by zero condition occurs −
Live Demo
Module exceptionProg
Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
Dim result As Integer
Try
result = num1 \ num2
Catch e As DivideByZeroException
Console.WriteLine("Exception caught: {0}", e)
Finally
Console.WriteLine("Result: {0}", result)
End Try
End Sub
Sub Main()
division(25, 0)
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
Live Demo
Module exceptionProg
Public Class TempIsZeroException : Inherits ApplicationException
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
Public Class Temperature
Dim temperature As Integer = 0
Sub showTemp()
If (temperature = 0) Then
Throw (New TempIsZeroException("Zero Temperature found"))
Else
https://www.tutorialspoint.com/vb.net/vb.net_exception_handling.htm 3/5
6/16/23, 3:57 AM VB.Net - Exception Handling
When the above code is compiled and executed, it produces the following result −
Throwing Objects
You can throw an object if it is either directly or indirectly derived from the System.Exception
class.
You can use a throw statement in the catch block to throw the present object as −
Throw [ expression ]
Module exceptionProg
Sub Main()
Try
Throw New ApplicationException("A custom exception _ is being thrown here
Catch e As Exception
Console.WriteLine(e.Message)
Finally
Console.WriteLine("Now inside the Finally Block")
End Try
Console.ReadKey()
End Sub
End Module
https://www.tutorialspoint.com/vb.net/vb.net_exception_handling.htm 4/5
6/16/23, 3:57 AM VB.Net - Exception Handling
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/vb.net/vb.net_exception_handling.htm 5/5