Visual Basic Throw Statement

In visual basic, Throw is a keyword and it is useful to throw an exception manually during the execution of the program and we can handle those thrown exceptions using try-catch blocks based on our requirements.

 

The Throw keyword will raise only the exceptions that are derived from the Exception base class.

Visual Basic Throw Keyword Syntax

Following is the syntax of raising an exception using Throw keyword in visual basic.

 

Throw e;

Here, e is an exception that is derived from the Exception class and Throw keyword to throw an exception.

Visual Basic Throw Exception Example

Following is the example of using Throw keyword to throw NullReferenceException and handling thrown exceptions in a try-catch block in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        Try

            GetDetails()

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

        Console.ReadLine()

    End Sub

    Private Sub GetDetails()

        Dim name As String = Nothing

        If String.IsNullOrEmpty(name) Then

            Throw New NullReferenceException("Name is Empty")

        Else

            Console.WriteLine("Name: " & name)

        End If

    End Sub

End Module

If you observe the above example, the GetDetails() method will throw NullReferenceException using the Throw keyword whenever the name variable value is empty or null. 

 

Here, we used a New keyword in Throw statement to create an object of valid exception type and we used a try-catch block in method caller to handle thrown exception.

 

When we execute the above example, we will get the result as shown below.

 

Name is Empty

This is how we can throw an exception in visual basic using Throw keyword based on our requirements.

Visual Basic Re-throw Exception

By using Throw keyword in the Catch block, we can re-throw an exception that is handled in the Catch block. The re-throwing an exception is useful when we want to pass an exception to the caller to handle it in a way they want.

 

Following is the example of re-throwing an exception to the caller using Throw keyword with try-catch blocks in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        Try

            GetDetails()

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

        Console.ReadLine()

    End Sub

    Private Sub GetDetails()

        Dim name As String = Nothing

        Try

            If name.Length > 0 Then

                Console.WriteLine("Name: " & name)

            End If

        Catch ex As Exception

            Throw

        End Try

    End Sub

End Module

If you observe the above example, in GetDetails() method we used a Throw keyword in the Catch block to re-throw an exception to the caller. In Main() method, we used a try-catch statement to handle the re-thrown exception.

 

To re-throw an exception, we need to use the only Throw keyword without any exception parameter. In case, if we try to re-throw an exception using exception parameter like Throw e, then the stack trace of original exception will not be preserved so if you want to re-throw an exception, then don’t use any exception parameter with Throw keyword.

 

When we execute the above program, we will get the result as shown below.

 

Object reference not set to an instance of an object.

This is how we can re-throw an exception to the caller using Throw keyword in Catch block based on our requirements.