VB.Net

VB.Net Exception Handling

VB.Net Exception Handling

An exception is a problem that arises during the execution of a program. An exception is a response to an exceptional circumstance that arises. At the same time, a program runs, such as dividingetcFirst, let usupdatereadexecute commands and retrieve data, store to connect comprises, when you declare itYouMicrosoft documentation can obtain a when you pass parameters by referenceetczero.

Exceptions provide a way to transfer control from one part of a program to another. VB.Net exception handling is built upon four keywords - Try, Catch, Finally and Throw.

  • Try − A Try block identifies a block of code for which particular exceptions will be activated. One or more Catch blocks follow it.
  • Catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The Catch keyword indicates the catching of an exception.
  • Finally − The Finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.
  • Throw − A program throws an exception when a problem shows up. This is done using a Throw keyword.

Syntax

Assuming a block will raise an exception, a method catches an exception using a combination of the Try and Catch keywords. A Try/Catch block is placed around the code that might generate an exception. Code within a Try/Catch block is referred to as protected code, and the syntax for using Try/Catch looks like the following −

Try   

[ tryStatements ]

   [ Exit Try ]

[ Catch [ exception [ As type ] ] [ When expression ]

   [ catchStatements ]

   [ Exit Try ] ]

[ Catch ... ]

[ Finally

   [ finallyStatements ] ]

End Try

You can list down multiple catch statements to catch a different types of exceptions in case your try block raises more than one exception in different situations.

Exception Classes in .Net Framework

Exceptions are represented as classes in the VB.Net Framework. The exception classes in the VB.Net framework are mostly derived directly or indirectly from the System.Exception class. The types System.ApplicationException and System.SystemException is some of the classes derived from System.Exception class.

The System.ApplicationException class handles errors thrown by application programs. As a result, the exceptions specified by programmers should be derived from this class.

The System.SystemException class acts as a base class for all system exceptions that are predefined.

Listed below are some predefined exception classes which are derived from the Sytem.SystemException class −

Exception Class

Description

System.IO.IOException

I/O errors are handled.

System.IndexOutOfRangeException

Handles errors occur when a method refers to an array index that is out of range.

System.ArrayTypeMismatchException

Handles errors that occur when the type does not match the array type.

System.NullReferenceException

Handles problems caused by deferring to a null object.

System.DivideByZeroException

Handles problems caused by dividing a dividend by zero.

System.InvalidCastException

Handles mistakes that occur during typecasting.

System.OutOfMemoryException

Handles errors caused by a lack of free memory.

System.StackOverflowException

Handles errors caused by stack overflow.

Handling Exceptions

In the form of try and catch blocks, VB.Net gives a structured answer to exception handling concerns. The core program statements are separated from the error-handling statements using these blocks.

TryCatch & Finally keywords are used to implement error handling blocks.

Creating User-Defined Exceptions:

Users can also define exceptions that are derived from the ApplicationException class. 

Throwing Objects

If an object is directly or indirectly derived from the 'System.Exception class,' you can throw it.

In the catch block, you can use a throw statement to throw the current object as

Throw [ expression ]

Top course recommendations for you

    Hashing in Java
    2 hrs
    Intermediate
    6.5K+ Learners
    4.45  (202)
    Eclipse Tutorial
    1 hrs
    Beginner
    3K+ Learners
    4.51  (148)
    NoSQL Database
    2 hrs
    Beginner
    12.3K+ Learners
    4.51  (1022)
    SQL Triggers for Beginners
    1 hrs
    Intermediate
    13K+ Learners
    4.48  (615)
    WordPress with Docker
    1 hrs
    Beginner
    9.1K+ Learners
    4.52  (527)
    C++ Tutorial
    2 hrs
    Beginner
    49.4K+ Learners
    4.42  (3057)
    OOPs Concepts in C++
    2 hrs
    Beginner
    31.4K+ Learners
    4.42  (2167)
    Robotic Process Automation
    3 hrs
    Beginner
    13.3K+ Learners
    4.6  (759)
    Docker Swarm
    1 hrs
    Beginner
    1.8K+ Learners
    4.5  (126)
    Factorial Program in C
    2 hrs
    Beginner
    5.2K+ Learners
    4.46  (556)