This article covers the concept of Exception Handling in VB.NET.
What are exceptions?
An exception refers to an event that is triggered during the execution of a program that goes against the flow, disrupting the program. An exception in VB.NET is meant to represent a error that occurs due to compiler being unable to handle an unexpected situation. As a result, the program flow is stopped and terminated.
General Syntax
Below is the general form for the Try and Catch keywords in exception handling. The use of both these keywords is compulsory, as they both go hand in hand. There are other optional keywords such as Finally and Throw related to exception handling that we will discuss further in this article.
Try
// Coding that will potentially throw an error
Catch obj as ExceptionType
// Code to be executed if Exception1 is thrown
Catch obj as ExceptionType
// Code to be executed if Exception2 is thrown
.
.
End Try
The dots in the above code represent the fact that there may be any number of catch statements included in the code. Each catch statement catches a single exception mentioned as ExceptionType
in the code above. Whenever an exception is thrown, it’s value is passed into the variable obj
(obj is an arbitrary name).
Remember that only a maximum of one Catch
block may trigger. It’s not possible for multiple Catch
blocks to trigger in the same run. It’s also possible that no catch blocks will trigger.
Example 1
A simple, bare bone example of Exception handling in VB.NET. The following code will “try” to execute divide a number by zero, but will produce a Zero Division Error and be caught by the Catch statement.
Module Module1
Sub Main()
Dim x As Integer = 6
Try
Console.WriteLine(x \ 0)
Catch ex As DivideByZeroException
Console.WriteLine("Zero Division Error detected")
End Try
Console.WriteLine("Program continuing....")
End Sub
End Module
We used the \
divide operator instead of the normal /
operator because VB.NET will return infinity for any number divided by zero if /
is used.
Zero Division Error detected
Program continuing....
The purpose of the last Console.writeline
statement was simply to demonstrate that the program execution goes beyond the try and catch
block. This would not true if we had not implemented Exception Handling.
The value of the exception is stored within the variable ex
.
Example 2
We’ll bring in two new concepts in this example. The Finally code block and the ability to block all exceptions with a single catch block. Notice, that instead of writing DivideByZeroException we simply wrote Exception. This allows each and every exception to be caught.
For this reason, if you have multiple Catch statements, keep this at the bottom else it will activate first not giving the others a chance to be checked.
Module Module1
Sub Main()
Dim x As Integer = 6
Try
Console.WriteLine(x \ 0)
Catch ex As Exception
Console.WriteLine("Error detected")
Finally
Console.WriteLine("Try Catch Ended")
End Try
Console.WriteLine("Program continuing....")
End Sub
End Module
The Finally is a code block that runs regardless of the outcome. Just think of it as a block that always runs. See the output below for it’s output.
Error detected
Try Catch Ended
Program continuing....
Raising Exceptions
There may come a time where you want to raise exceptions manually. Perhaps something in your program wasn’t going the way you wanted, but since it technically doesn’t count as an error, nothing happens. This is where you can insert a Throw keyword to manually raise an exception of your choosing.
The syntax is Throw New ExceptionType. Whichever exception you wish to raise, you can insert it in the place of ExceptionType.
Module Module1
Sub Main()
Try
Throw New Exception
Catch ex As Exception
Console.WriteLine("Exception thrown")
End Try
End Sub
End Module
Exception thrown
This marks the end of the VB.NET Exception Handling Article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be directed to the comments section below.