O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Throw Statement

Syntax

Throw exception
exception (required; an Exception object or an object derived from Exception)

An Exception object representing the exception being thrown

Description

Throws an exception that can be handled using either structured exception handling (a Try . . . Catch block) or unstructured exception handling (the On Error statement)

Example

Try
    ' Ask for a positive number
    Dim DataCt As Integer = CInt(InputBox("Enter number of items."))
    ' Check for error
    If DataCt <= 0 Then
        ' Throw an exception
        Throw New Exception("Must enter a positive number.")
    End If
Catch ex As Exception
    MsgBox(ex.Message)
End Try

VB.NET/VB 6 Differences

The Throw statement is new to VB.NET.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required