ARTICLE

How to use the Err.Number in VB.NET

Posted by Sapna Articles | Visual Basic 2010 April 07, 2011
Err.Number is a new error handling features and a properties to identify specific runtime errors.
 
Reader Level:


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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor