VB.Net/Development/Exception Yours
Содержание
A user-defined exception class
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() " catch any NegativeNumberException thrown Try Throw New NegativeNumberException("Exception occurred") Catch formatException As FormatException Console.WriteLine(formatException.Message) " diplay MessageBox if negative number input Catch negativeNumberException As _ NegativeNumberException Console.WriteLine(negativeNumberException.Message) End Try End Sub " Main
End Class Public Class NegativeNumberException
Inherits ApplicationException Public Sub New() MyBase.New("Illegal operation for a negative number") End Sub " New " constructor for customizing error message Public Sub New(ByVal messageValue As String) MyBase.New(messageValue) End Sub " New " constructor for customizing error message and specifying " inner exception object Public Sub New(ByVal messageValue As String, _ ByVal inner As Exception) MyBase.New(messageValue, inner) End Sub
End Class
</source>
Define and Use your own Exception Class
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) 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") " most derived exception type first Catch e As System.DivideByZeroException Console.WriteLine( _ "DivideByZeroException! Msg: {0}", e.Message) Console.WriteLine("HelpLink: {0}", e.HelpLink) " catch custom exception Catch e As MyCustomException Console.WriteLine( _ "MyCustomException! Msg: {0}", e.Message) Console.WriteLine("HelpLink: {0}", e.HelpLink) Catch " catch any uncaught exceptions Console.WriteLine("Unknown exception caught") Finally Console.WriteLine("Close file here.") End Try End Sub " do the division if legal Shared Public 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 " create a custom exception instance 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
End Class
Public Class MyCustomException Inherits System.ApplicationException Public Sub New(ByVal message As String) " pass the message up to the base class MyBase.New(message) End Sub "New End Class "MyCustomException </source>
Your own Exception Class
<source lang="vbnet"> Imports System Imports System.Text Imports System.Text.RegularExpressions
Public Class MainClass
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") " most derived exception type first Catch e As System.DivideByZeroException Console.WriteLine( _ "DivideByZeroException! Msg: {0}", e.Message) Console.WriteLine("HelpLink: {0}", e.HelpLink) " catch custom exception Catch e As MyCustomException Console.WriteLine( _ "MyCustomException! Msg: {0}", e.Message) Console.WriteLine("HelpLink: {0}", e.HelpLink) Catch " catch any uncaught exceptions Console.WriteLine("Unknown exception caught") Finally Console.WriteLine("Close file here.") End Try End Sub "Main Shared Public Function DoDivide(ByVal a As Double, ByVal b As Double) As Double If b = 0 Then Throw New System.DivideByZeroException( ) End If If a = 0 Then Throw New System.ArithmeticException( ) End If Return a / b End Function
End Class
Public Class MyCustomException Inherits System.ApplicationException Public Sub New(ByVal message As String) MyBase.New(message) End Sub End Class </source>
Your own Object Expired Exception
<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass
Shared Sub Main(ByVal args As String()) Try Throw New ObjectExpiredException("This object has expired.") Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub
End Class Public Class ObjectExpiredException
Inherits System.ApplicationException " No parameters. Use a default message. Public Sub New() MyBase.New("This object has expired") End Sub " Set the message. Public Sub New(ByVal new_message As String) MyBase.New(new_message) End Sub " Set the message and inner exception. Public Sub New(ByVal new_message As String, ByVal inner_exception As Exception) MyBase.New(new_message, inner_exception) End Sub
End Class
</source>