What is an Exception in VB.Net? What is Exception Handling in VB.Net? Syntax of Try Catch in VB.Net Exception Handling Example in VB.Net with Try Catch and Finally User Defined Exceptions in VB.Net Throw Exception in VB.Net

Syntax of Try Catch in VB.Net

The Try Catch statements in VB.Net are defined using the below syntax: The Try/Catch block should surround the code that may raise an exception. This code is known as a protected code. You can use multiple catch statements when you need to catch various types of exceptions.

Exception Handling Example in VB.Net with Try Catch and Finally

With the Try/Catch statements, you can separate your ordinary program code from the error-handling System. Let us demonstrate how to handle an exception using the Try, Catch, and Finally keywords. Step 1) Create a new console application. Step 2) Use this code: Step 3) Click the Start button from the toolbar to execute the code. You should get the following output:

Here is a screenshot of the code:

Explanation of Code:

Creating a module named Module1. Creating a sub-procedure named divisionFunction with two integer arguments n1 and n2. The ByVal is a VB.Net keyword stating that the values of the arguments will be passed by value. Creating an integer variable named answer. Start of the Try/Catch block. We need to surround the code that we suspect may raise an exception with this block. Performing a division operation between the values of variables n1 and n2. The result of the division operation is assigned to the variable answer. This code may raise an exception, for example, when we divide a number by zero, hence we have surrounded it with the Try/Catch blocks. Catching the Exception named DivideByZeroException and assigning the generated exception message to the ex. Printing some text on the console to notify the user of the type/name of the Exception that was raised. The Finally block. This will execute whether an exception is caught or not. Printing some text on the console showing the result of the division operation. End of the Try block, that is, end of the error-handling code. End of the divisionFunction sub-procedure. Start of the main sub-procedure. Calling/invoking the divisionFunction sub-procedure. The values of the two integer arguments are passed here. The value of n1=4 and n2=0. This means the division operation will be 4\0, that is, n1\n2. Pausing the console window waiting for the user to take action to close it. End of the main sub-procedure. End of the module.

User Defined Exceptions in VB.Net

VB.Net allows you to define your own exceptions. You can get user-defined exception classes from ApplicationException class. Let us demonstrate this by an example:

Example of User Defined Exception in VB.Net

Here is an example of user defined exception in VB.Net: Step 1) Create a new console application. Step 2) Use the following code: Step 3) Click the Start button from the top bar to execute the code. You should get the following output:

Here is a screenshot of the code:

Explanation of Code:

Creating a module named Module1. Creating an exception class named HeightIsZeroException. The class inherits all the ApplicationException class. Creating a sub-procedure named New. It will take one string argument named text. Calling the base class constructor and passing to it the above argument. End of the New sub-procedure. End of the HeightIsZeroException class. Creating a class named Height. Its public access modifier is Public, meaning that it will be publicly accessible. Creating an integer variable named Height and initializing its value to 0. Creating a sub-procedure named showHeight(). Checking for when the value of the variable Height is 0. We have used the If…Then condition. To Throw the HeightIsZeroException if the above condition is true. The message Zero Height Found will be shown when this Exception is thrown. The Else part to be executed when the If condition is not true. Message to print on the console showing the Height when it is not 0. End of the If condition. End of the showHeight() subprocedure. End of the class Height. Start of the main sub-procedure. Creating an object named Height as an instance of the Height class. Start of the exception handling block. Invoking the showHeight() sub-procedure defined in the Height class. We have used an object of this class to access this method. This line may result into an exception. Therefore, we have surrounded it with an exception handling code. Catching the HeightIsZeroException Exception as object ex in case it occurs. The message to print on the console when the HeightIsZeroException is caught. End of the exception handling block. Pause the console window waiting for a user to take action to close it. End of the main sub-procedure. End of the module.

Throw Exception in VB.Net

In exception handling, you can choose to throw an object. However, the object must be derived from System. Exceptionclass, directly or indirectly. For example:

Example of Throw Exception in VB.Net

Below is an example of throw exception in VB.Net: Step 1) Create a new console application. Step 2) Use the following code: Step 3) Click the Start button from the top bar to execute the code. You should get the following:

Here is a screenshot of the code:

Explanation of Code:

Creating a module named Module1. Start of the main sub-procedure. Start of the error handling code. Throwing an exception object. This code may generate an exception hence we have surrounded it with a Try/Catch block. Catching an exception as object ex if the line throws an exception. Printing the exception message on the console in case it occurs. The Finally block to run whether an exception is caught or not. The message to print on the console when the Finally block is executed. End of the exception handling code. Pause the console for a while waiting for a user to take action to close it. End of the main sub-procedure. End of the module.

Summary

An exception refers to a problem that arises during program execution brought about by an unexpected circumstance. If you suspect that some code will generate an exception, surround it with a Try/Catch block. The Finally block comes after the Try/Catch block and executes whether an exception is caught or not. VB.Net allows us to create custom exceptions.