1

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

1 Answer 1

2

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 from Exception via parent hierarchy. For example, see the following inheritance tree:

Exception
|
|-MyGenericException
|  |-MyFooException
|  |-MyBarException
|
|-OtherGenericException  
   |-OtherFooException
   |-OtherBarException

See, that some exception classes do not directly inherit from the Exception, however they have a parent class which, in tern, is derived from the Exception.

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 are MyIOException and MySecurityException. The other four less general classes derive from them.

//------------ Networking
public class MyIOException : Exception
{
    public string AdditionalData {get; set;}
}
public class MyNetworkFailureIOException : MyIOException
{
    public string Reason {get; set;}
}
public class MyRemoteFileNotFoundIOException : MyIOException
{
    public string RemotePath {get; set;}
}

//------------ Security
public class MySecurityException : Exception
{
    public string UserName {get; set;}
}
public class MyAccessDeniedException : MySecurityException
{
    public string PolicyName {get; set;}
}
public class MyUnauthorizedException : MySecurityException
{
    public string CodeName {get; set;}
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Smith see my edit. Sorry I am not a VB guy so the code is in C#. Hope it make sense.
this is the exact thing am trying to avoid, creating a class for each exception type MyUnauthorizedException,MySecurityException,MyAccessDeniedException
I would never recommend it, but you could also just throw MySecurityException with string field like MySecurityException.DetailedName in which you place a more specified exception type like 'Unauthorized'. But this is not the OOP way and will bite you in the behind later on! I highly recommend oleksii's approach in this. Go with it, even though it is a bit more work.
@Smith, well it's your design show. If you feel like you need 20 different classes to describe your domain, then there is nothing bad in it. But if all of those would just have different names and the same structure (same properties, ctors etc) it's better not to do a copy-paste code. Think about maintenance. You can have a handful number of exception and just provide properties like Reason, AdditionalInformation, ErrorDescription - you know the general stuff. To me 20 different exceptions would be required by a complex enterprise system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.