How to VB.NET Exceptions

Exceptions in .NET are occurrences of conditions that disrupt the normal flow of program execution. They represent runtime errors that can arise due to various reasons such as running out of memory, encountering file-related issues, or network connectivity problems.

Exception Handling

In .NET languages, structured exception handling is a crucial aspect of the Common Language Runtime (CLR). It offers several advantages over the previous On Error statements used in earlier versions of Visual Basic. All exceptions in the CLR are derived from a common base class called Exception. This provides a unified approach for handling exceptions in a consistent manner.

You can handle Exceptions using Try..Catch statement.

Try code exit from Try Catch [Exception [As Type]] code - if the exception occurred this code will execute exit from Catch

The .NET framework provides a range of predefined exception classes to represent different types of errors. These classes are organized in a hierarchy, allowing for specific exception handling based on the type of error encountered. Additionally, developers have the flexibility to create their own custom exception classes by inheriting from the base Exception class. This enables the creation of application-specific exceptions tailored to specific scenarios.

Finally

In structured exception handling, the finally block is a code section that executes regardless of whether an exception is thrown or not. It ensures that certain code statements are executed, regardless of the outcome of the try-catch block.

The finally block is typically used to perform cleanup operations or release resources that were acquired in the try block, ensuring that they are properly handled regardless of any exceptions that may have occurred.

The code in the finally block will execute after the try block completes its execution, whether an exception was thrown or not. If an exception is thrown and caught by a catch block, the code in the catch block will execute before the finally block. Once the catch block (if present) is executed, the control will then transfer to the finally block.

Try code exit from Try Catch [Exception [As Type]] code - if the exception occurred this code will execute exit Catch Finally code - this code should execute , if exception occurred or not

The following example trying to divide a number by zero.

Try Dim i As Integer Dim resultValue As Integer i = 100 resultValue = i / 0 MsgBox("The result is " & resultValue) Catch ex As Exception MsgBox("Exception catch here ..") Finally MsgBox("Finally block executed ") End Try

From the following VB.NET code , you can understand how to use try..catch statements. Here we are going to divide a number by zero .

Full Source VB.NET
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Try Dim i As Integer Dim resultValue As Integer i = 100 resultValue = i / 0 MsgBox("The result is " & resultValue) Catch ex As Exception MsgBox("Exception catch here ..") Finally MsgBox("Finally block executed ") End Try End Sub End Class

When you execute this program you will "Exception catch here .." first and then "Finally block executed " . That is when you execute this code , an exception happen and it will go to catch block and then it will go to finally block.

Conclusion

Developers can catch and handle exceptions, providing appropriate error messages and taking necessary actions to handle exceptional conditions. This helps in improving the robustness and reliability of the application, ensuring that it can recover from unexpected errors and continue functioning correctly.