By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Custom exception constructors

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

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

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.