Use the Err.Number in VB.NET

Err.Number is a new error handling features and a properties to identify specific runtime errors.
  • 7643

Err.Number is a new error handling features and a properties to identify specific runtime errors, the only way to capture errors in the VBScript was using the "On Error Resume Next" statement and checking each line for an error with "If Err.Number <> 0 Then " statements. When returning a user-defined error from an object, set Err.Number by adding the number you selected as an error code to the VbObjectError constant.

The Following table lists many of the runtime errors that Visual Basic application can encounter:

 

Error Number Default Error Message
5 Procedure call or argument is not valid
6 Overflow
7 Out of memory
11 Division by zero
51 Internal error
52 Bad file name or number
53 File not found
55 File already open
76 Path not found
482 Printer error

Example:

Imports System
Public
Class MainClass
    Shared Sub
Main(ByVal args As String())
        Dim I As Integer
        On Error Resume Next
        I = Value()
        Select Case Err.Number
            Case 0
            Case 11
                I = 100
            Case Else
                Console.WriteLine("Error X." & vbCrLf & Err.Description)
                Console.ReadLine()
                Exit Sub
        End Select
        Console.WriteLine("I = " & I)
        Console.ReadLine()
    End Sub
    Private Shared Function Value() As Integer
        Return 1 \ Integer.Parse("0")
    End Function
End Class

Output:
error.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.