Introduction of Exception Handling in VB .net
-
Exception:
-
An Exception, in OOP programming, is a hidden, unwanted, abnormal unexpected, or exceptional event that occurs during the execution of a program and interrupts the normal flow of the program.
-
When a program encounters an exception, it stops executing the current code block and immediately transfers the control to a special piece of code called an exception handler. The exception handler typically handles the exception by providing an appropriate response, such as displaying an error message or logging the error for further analysis.
-
An Exception may be of System exception or an Application exception.
-
Exceptions can occur for a variety of reasons, such as invalid input data, unexpected program behavior, or resource limitations.
-
By using exceptions, programmers can write more robust and fault-tolerant code that can handle unexpected situations and recover from errors.
-
-
Exception handling is a critical concern of OOPs programming language, including VB .NET.
-
VB .NET provides a robust set of features for handling exceptions, which helps the developer write more reliable and efficient codes.
Definition of Exception Handling in VB .net
- In VB .NET, exception handling is a programming mechanism that enables programmers to handle run time sudden errors and other exceptional conditions.
- Exception handling is an OOP programming concept that refers to the process of identifying, catching, and responding to errors or exceptional events that occur during the execution of a program.
Features of Exception Handling in VB .net
- The purpose of exception handling in VB .NET is to provide a structured and reliable way to handle errors, prevent crashes, and recover from unexpected situations that may arise during program execution.
- By handling exceptions, developers can provide a better user experience, improve application stability, and help prevent data loss or corruption. In other words, the goal of exception handling is to improve the reliability and robustness of software applications by providing a mechanism to gracefully recover from errors and unexpected events.
- Additionally, VB .NET also provides several other error-handling constructs, such as the On Error statement and the Throw statement, which can be used in conjunction with the Try-Catch-Finally block to provide more advanced error-handling capabilities.
Structure of Exception Handling in VB .net
- When an exceptional event, such as an error, occurs in a program, an exception is thrown, and the program can choose to handle the exception in a way that prevents the program from crashing or producing incorrect results.
- In VB .NET, exceptions are objects that are created when a run time error occurs, and they contain information/messages about the error, such as its type and message.
- In VB .Net, exception handling may be performed using four structured error-handling constructs /built-in keywords: Try, Catch, Finally, and Throw.
Try
-
- The Try block is compulsory.
- The Try block is the region of the program where the main code is written and these codes may throw an exception, if any.
Catch
-
- The Catch block is compulsory.
- This block accepts the exception information thrown by the Try block.
- The Catch block contains the code that is executed when an exception is caught.
Finally
-
- The Finally block is optional and is used to specify code that should always be executed, regardless of whether an exception occurred or not.
- Exception Classes in VB .Net :
-
In VB .Net, All the exception classes originate from the parent’s class ‘System. Exception’.
-
In VB .Net, there are two main exception classes used primarily to handle various types of exceptions.
- System.SystemException
- System.ApplicationException
System.SystemException: (Pre-defined/In-Built Exception) This exception class includes all predefined and some system-generated exception classes that arise during the run time. For example – DivideByZeroException, IndexOutOfRangeException, StackOverflowExpression, and so on.
System.ApplicationException: (User-defined Exception) This exception class throws an exception defined within the application by the programmer or developer. This exception was created by inheriting from System.ApplicationException class.
-
Syntax of Exception Handling in VB .net
- General form of Syntax is
Try
‘ Programmer Codes that may throw an exception
Catch ex As Exception
‘ Codes to handle the exception
Finally
‘ Code that should always be executed
End Try
OR
Try
Codes or Statements
[ Exit Try ] ‘If necessary
Catch [ Exception name] As [ Exception Type]
[Catch Statements] Catch [Exception name] As [Exception Type]
[ Exit Try ] ‘If necessary
Finally
[ Finally Statements ]
End Try
- In VB.NET, we can create a customized Catch statement to handle specific exceptions based on their type, message, or other criteria. We can use the ‘When’ keyword to specify a condition that must be met for the Catch block to execute. For example –
Try
‘ Main codes that might throw an exception.
Catch ex As Exception When ex.Message.Contains(“Invalid”)
‘ Codes to handle exceptions with “Invalid” in the error message.
Catch ex As Exception When TypeOf ex Is FileNotFoundException
‘ Codes to handle FileNotFoundExceptions.
Finally
‘ Codes that always execute, regardless of whether an exception was thrown.
End Try
- We can also include multiple Catch blocks to handle different types of exceptions. For example:-
Try
‘ Main codes that might throw an exception
Catch ex As FileNotFoundException
‘ Codes to handle the specific “file not found” exception
‘ Codes to handle IOExceptions
Catch ex As ArgumentException
‘ Codes to handle ArgumentExceptions
Catch ex As Exception
‘ Codes to handle all other/general exceptions
End Try
- We can also use the Throw statement to manually throw an exception. This can be useful for testing exception handling code or for creating custom exceptions. For example:-
Throw New ApplicationException(“A customized error message occurred!”)
- Nested Try/Catch Blocks Statement: The nested try/catch blocks are used to handle exceptions at different levels of code execution.
Try
‘ some code that might throw an exception
Try
‘ some nested code that might throw a different exception
Catch ex As Exception
‘ handle nested exception here
End Try
Catch ex As Exception
‘ handle all other exceptions here
End Try
NB: If an exception occurs that doesn’t match any of the specified types, it will be caught by the final/last catch block.
Working Mechanism of Exception Handling in VB .net
The overview of how exception handling works in VB .NET is :-
- The Try block contains the major code of the program that may cause an exception to be thrown. When an error occurs during the execution of a block of code, VB .NET throws an exception.
- The exception is an object that contains information about the error, such as its type and message.
- Now the thrown exception is caught and handled gracefully using the Catch block/statement i.e. the Catch block is executed when an exception is thrown. It contains the code that handles the exception.
- The Finally block is executed regardless of whether an exception is thrown or not. It contains the code that must be executed, regardless of whether an exception occurs.
0 Comments