VB.NET supports structured exception handling similar to that seen in C++. In
structured exception handling, you write code surrounded by blocks. If an
exception occurs, the block throws the execution control to a predefined handled
code. The try, catch, finally statements define these blocks. In other words, if
an application handles exceptions that occur during the execution of a block of
application code, the code must be placed within a try statement. Application
code within a try statement is a try block. Application code that handles
exceptions thrown by a try block is placed within a catch statement and is
called a catch block. In a try block, you define the code you want to execute
and protect from any possible errors. The catch block defines the handlers for
the exception. The finally block is used to free resources allocated in the try
block.
The try statement provides a mechanism to capture exceptions in a block of code
and a mechanism to execute blocks of code both under normal circumstances and
when an exception occurs.
You can define a try statement in a few different forms:
-
try-catch
-
try-finally
-
try-catch-finally
Let's discuss each one of them below.
Try-Catch
In try-catch, a try block is followed by one or more catch blocks. You basically
surround the block of code where you expect an error to occur with the try
statement followed by one or more catch blocks to handle one or more types of
exceptions. Let's look at the example below to understand it better:
Exception1.VB, Try-Catch Example
Imports
System
Imports
System.Text
Public Class LearnAboutTryCatch1
Shared Sub
GenerateException(ByVal
arg1 As Int32,
ByVal
arg2 As Int32)
Dim result1 As Int32
= 0
Try
'BLOCK OF CODE WHERE ERRORS ARE EXPECTED
'division
result1 = arg1 / arg2
'multiplication
Console.WriteLine(result1)
Catch e As
System.DivideByZeroException
'HANDLE THE ERROR
'StackTrace gives us the location of error as
' string
'e.Message: gives the error message, in this
' case, Attempted to divide by zero.
'e.Stacktrace: gives the location of the error
'in text form. In this case at
' LearnAboutTryCatch1.GenerateDivideByZeroError(Int32 Numerator,Int32
Denominator)
'in this case the output would be
'Attempted to divide by zero. at
'LearnAboutTryCatch1.GenerateDivideByZeroError(
' Int32 Numerator,Int32 Denominator)
Console.WriteLine([String].Concat(e.Message,
e.StackTrace))
Catch e As
System.OverflowException
'HANDLE THE ERROR
'StackTrace gives us the location of error as
' string
'e.Message: gives the error message in this case,
'e.Stacktrace: gives the location of the
' error in text form.
Console.WriteLine([String].Concat(e.Message,
e.StackTrace))
End Try
End Sub
Shared Sub
Main()
GenerateException(6, 0)
Console.ReadLine()
End Sub
End Class
Output of above example:

First we create the class LearnAboutTryCatch1, which has the method
GenerateException that divides two numbers. The block of code where we would
expect errors in this case, the division operation is surrounded by the try
clause. The try clause is followed by a catch statement, which has an argument
of type System.DivideByZeroException to catch divide-by-zero errors. If you
divide a number by zero, this causes a divide-by-zero exception.
You have to handle all of the exceptions inside the catch clause. In this
example, the block of code inside the catch clause writes a message to console.
Note that we have concatenated the Message property and the StackTrace property
of the exception to generate the message. The Message property gives the actual
error message, and the StackTrace gives the location of the actual error.
Finally in the Main() method, we call the function by passing 6 and 0 to
generate a divide-by-zero error. Now, in next example, we will modify the code
above to illustrate how to use multiple catch statements:
Exception2.VB, Second Exception Example
Imports System
Imports
System.Text
Public Class LearnAboutTryCatch2
Public
Shared Sub
GenerateException(ByVal
arg1 As Int32,
ByVal
arg2 As Int32)
Dim
result1 As Int32
= 0
Dim result2 As Int32
= 0
Try
'BLOCK OF CODE WHERE ERRORS ARE EXPECTED
'division
result1 = arg1 / arg2
'multiplication
result2 = arg1 * arg2
Console.WriteLine(result1)
Console.WriteLine(result2)
Catch e As
System.DivideByZeroException
'HANDLE THE ERROR
'StackTrace gives us the location of error as
' string
'e.Message: gives the error message, in this
' case, Attempted to divide by zero.
'e.Stacktrace: gives the location of the error
'in text form. In this case at
' LearnAboutTryCatch1.GenerateDivideByZeroError(Int32 Numerator,Int32
Denominator)
'in this case the output would be
'Attempted to divide by zero. at
'LearnAboutTryCatch1.GenerateDivideByZeroError(
' Int32 Numerator,Int32 Denominator)
Console.WriteLine([String].Concat(e.Message,
e.StackTrace))
Catch e As
System.OverflowException
'HANDLE THE ERROR
'StackTrace gives us the location of error as
' string
'e.Message: gives the error message in this case,
'e.Stacktrace: gives the location of the
' error in text form.
Console.WriteLine([String].Concat(e.Message,
e.StackTrace))
End Try
End Sub
Public Shared Sub
Main()
GenerateException(6, 0)
GenerateException(999999999, 999999999)
Console.ReadLine()
End Sub
End Class
Output of above example:

We have added a multiplication operation inside the GenerateException function,
and we have also added one more catch clause to handle System.OverflowException,
which could result from multiplying two big numbers. For example, if the
resulting value of the multiplication is bigger than what an Int32 type can
hold, we would expect to get an overflow exception so we have to add one catch
block for each exception. Hence, this example above, demonstrates how to
handle more than one type of exception.
Assume that we have passed 999999999 as arg1 and 999999999 as arg2 to the
GenerateException method. Note that result2 is Int32 type, and when we multiply
999999999 and 999999999, we get a stack overflow error.
Compile the code as usual with the following command:
csc LearnAboutTryCatch2.vb.
Once you run LearnAboutTryCatch2.exe, you would expect that both the
divide-by-zero and the overflow exceptions would be thrown. However, you would
be surprised to know that only the divide-by-zero exception is raised and not
the overflow exception because result1=arg1/arg2; code comes first.
Conclusion
Hope this
article would have helped you in understanding Exception Statements in VB.NET.