728x90
반응형
Example:
Imports System
Public Class Example
Private Shared Sub Main()
' Define some integers for a division operation.
Dim values = {10, 7}
For Each value In values
Try
Console.WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value))
Catch e As ArgumentException
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
End Try
Console.WriteLine()
Next
End Sub
Private Shared Function DivideByTwo(ByVal num As Integer) As Integer
' If num is an odd number, throw an ArgumentException.
If (num And 1) = 1 Then Throw New ArgumentException(String.Format("{0} is not an even number", num), "num")
' num is even, return half of its value.
Return num / 2
End Function
End Class
' This example displays the following output:
' 10 divided by 2 is 5
'
' ArgumentException: 7 is not an even number
' Parameter name: num
Example:
Public Sub SomeMethod()
If m_Disposed Then
Throw New ObjectDisposedException("Object has been disposed")
End If
' ... Normal execution code
End Sub
반응형
댓글