VB.Net/Development/Exception System

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

Parse Number Exception

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
       Dim num_items As Integer
       Try
           num_items = Integer.Parse("aa")
       Catch ex As Exception
           Console.WriteLine("Error in Num Items." & vbCrLf & ex.Message)
           Exit Sub
       End Try
       " Check that the value is between 1 and 100.
       If num_items < 1 Or num_items > 100 Then
           Console.WriteLine("Num Items must be between 1 and 100")
           Exit Sub
       End If
   End Sub

End Class

      </source>


Use Argument Exception

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main(ByVal args As String())
       Try
           Dim rect As New DrawableRectangle(10, 20, 0, -100)
       Catch ex As Exception
           Console.WriteLine(ex.Message)
       End Try
   End Sub

End Class Public Class DrawableRectangle

   Public Sub New(ByVal new_x As Integer, ByVal new_y As Integer, ByVal new_width As Integer, ByVal new_height As Integer)
       " Verify that new_width > 0.
       If new_width <= 0 Then
           " Throw an ArgumentException.
           Dim ex As New ArgumentException( _
               "DrawableRectangle must have a width greater than zero", _
                   "new_width")
           Throw ex
       End If
       " Verify that new_height> 0.
       If new_height <= 0 Then
           " Throw an ArgumentException.
           Throw New ArgumentException( _
               "DrawableRectangle must have a height greater than zero", _
                   "new_height")
       End If
   End Sub

End Class

      </source>