Export (0) Print
Expand All

10.10.2.3 Resume Statement

Visual Studio .NET 2003

A Resume statement returns execution to the statement that caused the most recent exception. If the Next modifier is specified, execution returns to the statement that would have been executed after the statement that caused the most recent exception. If a label name is specified, execution returns to the label.

Because the SyncLock statement contains an implicit structured error-handling block, Resume and Resume Next have special behaviors for exceptions that occur in SyncLock statements. Resume returns execution to the beginning of the SyncLock statement, while Resume Next returns execution to the next statement following the SyncLock statement. For example, consider the following code:

Imports System

Class LockClass
End Class

Module Test
    Sub Main()
        Dim FirstTime As Boolean = False
        Dim Lock As LockClass = New LockClass()

        On Error Goto Handler

        SyncLock Lock
            Console.WriteLine("Before exception")
            FirstTime = False
            Throw New Exception()
            Console.WriteLine("After exception")
        End SyncLock

        Console.WriteLine("After SyncLock")
        Exit Sub

Handler:
        If FirstTime Then
            Resume
        Else
            Resume Next
        End If
    End Sub
End Module

It prints the following result.

Before exception
Before exception
After SyncLock

The first time through the SyncLock statement, Resume returns execution to the beginning of the SyncLock statement. The second time through the SyncLock statement, Resume Next returns execution to the end of the SyncLock statement. Resume and Resume Next are not allowed within a SyncLock statement.

In all cases, when a Resume statement is executed, the most recent exception is set to Nothing. If a Resume statement is executed with no most recent exception, the statement raises a System.Exception exception containing the Visual Basic error number 20 (Resume without error).

ResumeStatement ::= Resume [ ResumeClause ] StatementTerminator
ResumeClause ::= Next | LabelName

See Also

10.10.2.1 Error Statement | 10.10.2.2 On Error Statement | 10.10.2 Unstructured Exception-Handling Statements | Resume Statement (Visual Basic Language Reference) | Unstructured Exception Handling (Visual Basic Language Concepts)

Show:
© 2014 Microsoft