VB.Net/Development/Err Number

Материал из VB Эксперт
Версия от 12:45, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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