i have about 20 possible exception messages that i want thrown when an error occurs.
i need somthng like this when catching the exception
Try
' do domthing
Catch ex As CustomInvalidArgumentException
'do domthing
Catch ex As CustomUnexcpectedException
'do domthing
Catch ex As Exception
'do domthing
End Try
currently i have a class like this
<Serializable()> _
Public Class CustomException
Inherits Exception
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
Public Sub New(ByVal format As String, ByVal ParamArray args As Object())
MyBase.New(String.Format(format, args))
End Sub
Public Sub New(ByVal message As String, ByVal innerException As Exception)
MyBase.New(message, innerException)
End Sub
Public Sub New(ByVal format As String, ByVal innerException As Exception, ByVal ParamArray args As Object())
MyBase.New(String.Format(format, args), innerException)
End Sub
Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
MyBase.New(info, context)
End Sub
End Class
do i have to create a class that inherits from Exception for each type of exception
No you don’t need to make each exception class to directly inherit from
Exception. But you need to make sure that all you custom exceptions can be derived fromExceptionvia parent hierarchy. For example, see the following inheritance tree:See, that some exception classes do not directly inherit from the
Exception, however they have a parent class which, in tern, is derived from theException.A sample code is in C# written in notepad but hopefully you can get the idea.
2 more general exception classes inherit from
Exception. They areMyIOExceptionandMySecurityException. The other four less general classes derive from them.