Programming Tutorials

Using Resume Next and Resume Line in VB.net

By: Steven Holzner in VB.net Tutorials on 2010-11-17  

One of the most useful aspects of unstructured exception handling is the Resume statement, which lets you resume program execution even after an exception has occurred. You can use Resume to resume execution with the statement that caused the exception, Resume Next to resume execution with the statement after the one that caused the exception, and Resume line, where line is a line number or label that specifies where to resume execution. Here's an example using Resume Next, which lets you skip over the line that caused the problem:

Module Module1
    Sub Main()
        Dim int1 = 0, int2 = 1, int3 As Integer
        On Error Goto Handler
        int3 = int2 / int1
        System.Console.WriteLine("Program completed...")
        Exit Sub
Handler:
        If (TypeOf Err.GetException() Is OverflowException) Then
            System.Console.WriteLine("Overflow error!")
            Resume Next
        End If
    End Sub
End Module

Here's what you see when you run this console application:

Overflow error!
Program completed...

And here's an example using the Resume line form:

Module Module1
    Sub Main()
        Dim int1 = 0, int2 = 1, int3 As Integer
        On Error Goto Handler
        int3 = int2 / int1
Nextline:
        System.Console.WriteLine("Program completed...")
        Exit Sub
Handler:
        If (TypeOf Err.GetException() Is OverflowException) Then
            System.Console.WriteLine("Overflow error!")
            Resume Nextline
        End If
    End Sub
End Module

You can also use an On Error Resume Next or On Error Resume line statement to make Visual Basic continue program execution after an exception has occurred. This form is sometimes preferable to the On Error GoTo form if you don't want to write an explicit exception handler:

Module Module1
    Sub Main()
        Dim int1 = 0, int2 = 1, int3 As Integer
        On Error Resume Next
        int3 = int2 / int1
        





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)