VB.Net Tutorial/Development/Custom Exception

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

Create Custom Exception class

Option Strict On
 Imports System
 Public Class MyCustomException
     Inherits System.ApplicationException
     Public Sub New(ByVal message As String)
         MyBase.New(message)
     End Sub
 End Class
 Class Tester
     Public Shared Function DoDivide(ByVal a As Double, ByVal b As Double) As Double
         If b = 0 Then
             Dim e As New DivideByZeroException( )
             e.HelpLink = "http://www.vbex.ru"
             Throw e
         End If
         If a = 0 Then
             Dim e As New _
               MyCustomException("Can"t have zero divisor")
             e.HelpLink = "http://www.vbex.ru"
             Throw e
         End If
         Return a / b
     End Function "DoDivide
     Shared Sub Main( )
         Try
             Console.WriteLine("Open file here")
             Dim a As Double = 0
             Dim b As Double = 5
             Console.WriteLine("{0} / {1} = {2}", a, b, DoDivide(a, b))
             Console.WriteLine("This line may or may not print")
         Catch e As System.DivideByZeroException
             Console.WriteLine("DivideByZeroException! Msg: {0}", e.Message)
             Console.WriteLine("HelpLink: {0}", e.HelpLink)
         Catch e As MyCustomException
             Console.WriteLine("MyCustomException! Msg: {0}", e.Message)
             Console.WriteLine("HelpLink: {0}", e.HelpLink)
         Catch
             Console.WriteLine("Unknown exception caught")
         Finally
             Console.WriteLine("Close file here.")
         End Try
     End Sub
 End Class
Open file here
MyCustomException! Msg: Can"t have zero divisor
HelpLink: http://www.vbex.ru
Close file here.

Create your own Exception class by subclassing System.Exception

Imports System.Runtime.Serialization
Public Class CustomException
   Inherits System.Exception
   Public Sub New()
      Me.New("An error occurred in the component.", Nothing)
   End Sub
   Public Sub New(message As String)
      Me.New(message, Nothing)
   End Sub
   Public Sub New(message As String, innerException As Exception)
      MyBase.New(message, innerException)
      Source = "My_Custom_Component"
   End Sub
   Protected Sub New(info As SerializationInfo, context As StreamingContext)
      MyBase.New(info, context)
   End Sub
End Class

Demonstrating a user-defined exception class

Public Class Tester
    Public Shared Sub Main
    
      Try
         Console.WriteLine(SquareRoot(123.123))
         Console.WriteLine(SquareRoot(-123.123))
      Catch formatException As FormatException
         Console.WriteLine(formatException.Message)
      Catch negativeNumberException As _
         NegativeNumberException
         Console.WriteLine(negativeNumberException.Message)
      End Try
    End Sub
   Public Shared Function SquareRoot(ByVal operand As Double) As Double
      If operand < 0 Then
         Throw New NegativeNumberException( _
            "Square root of negative number not permitted")
      End If
      Return Math.Sqrt(operand)
   End Function " cmdSquareRoot
End Class

Public Class NegativeNumberException
   Inherits ApplicationException
   Public Sub New()
      MyBase.New("Illegal operation for a negative number")
   End Sub " New
   Public Sub New(ByVal messageValue As String)
      MyBase.New(messageValue)
   End Sub " New
   Public Sub New(ByVal messageValue As String, _
      ByVal inner As Exception)
      MyBase.New(messageValue, inner)
   End Sub
End Class
11.0960803890383
Square root of negative number not permitted

Inherits System.ApplicationException

Imports System.Runtime.Serialization
public class Test
   public Shared Sub Main
        Try
            Throw New MyException("This is my Exception.")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
   End Sub
End class

Public Class MyException
    Inherits System.ApplicationException
    Public Sub New()
        MyBase.New("This object has expired")
    End Sub
    Public Sub New(ByVal new_message As String)
        MyBase.New(new_message)
    End Sub
    Public Sub New(ByVal new_message As String, ByVal inner_exception As Exception)
        MyBase.New(new_message, inner_exception)
    End Sub
    Public Sub New(ByVal info As SerializationInfo, _
    ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub
End Class
This is my Exception.