Custom exception constructors

  • Thread starter Thread starter Daniel Klein
  • Start date Start date
D

Daniel Klein

When creating a custom exception that derives from ApplicationException,
why is it necessary to have the 3 basic contructors, i.e.

Public Sub New()
Public Sub New(ByVal message As String)
Public Sub New(ByVal message As String, ByVal inner As Exception)

It seems that all of the texts say to do this but my custom exception will
have it's own constructors based on the application's requirements.

Is there some .NET reason to include these?

Thanks,
Daniel Klein
Dublin, Ohio
 
Hi Daniel
Is there some .NET reason to include these?

When instantiating a class that has been subclassed from a base class, the
base classes constructors are not available to the instantiating code.
Therefore if you want to have access to these overloaded constructors you
must recreating them in your inherited class and then call MyBase.New(blah,
blah) to initialize the base class.

The reason you see all examples with these three standard constructors for
exceptions is not so much technical as it is best practise. You dont have to
supply these constructors in ApplicationException subclasses but if you dont
then how are you going to provide the inner exception, message, etc
properties to the base class.

Other developers using your code will expect these properties to have
valuable information in them should your code throw an exception of your
custom type. Its basically a professionally courtesy to the next hack who
has to try and decipher why his/her code went bang!

If you dont supply this info, then expect your support costs to go up and
peoples time to be wasted.
These properties are very important for tracing and the like, to follow the
exception chain.

Only sociopathic developers intentionally decide not to include this
information in their custom exceptions.

hth
Richard
 
Back
Top