VB.Net/Development/Err Number — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:45, 26 мая 2010

Check Err.Number When there is an Error

Imports System
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim X As Integer
        " Try to calculate X.
        On Error Resume Next
        
        X = CalculateValue()
        
        Select Case Err.Number
            Case 0 " No error. Do nothing.
            Case 11 " Divide by zero. Set a default value.
                X = 1000
            Case Else " Unexpected error. Tell the user.
                Console.WriteLine("Error calculating X." & vbCrLf & Err.Description)
                Exit Sub
        End Select
        Console.WriteLine("X = " & X)
    End Sub
    
    Shared Private Function CalculateValue() As Integer
        Return 1 \ Integer.Parse("0")
    End Function
End Class


On Error Resume Next

Imports System
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        On Error Resume Next
        DoSomething()
        If Err.Number <> 0 Then
            Console.WriteLine("Error in DoSomething")
            Exit Sub
        End If
        DoSomethingElse()
        If Err.Number <> 0 Then
            Console.WriteLine("Error in DoSomethingElse")
            Exit Sub
        End If
    End Sub
    Shared Private Sub DoSomething()
            Throw New ArgumentException("Error in subroutine DoSomething")
    End Sub
    Shared Private Sub DoSomethingElse()
            Throw New ArgumentException("Error in subroutine DoSomethingElse")
    End Sub
End Class