PC Review


Reply
Thread Tools Rate Thread

Custom exception constructors

 
 
Daniel Klein
Guest
Posts: n/a
 
      12th Jul 2004
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
 
Reply With Quote
 
 
 
 
Richard Myers
Guest
Posts: n/a
 
      12th Jul 2004

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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Exception Raj Microsoft C# .NET 3 16th Oct 2009 06:39 AM
displaying exception message from custom exception bdgreen@pacbell.net Microsoft ASP .NET 2 18th Aug 2006 06:59 PM
Custom COM Exception =?Utf-8?B?U0RY?= Microsoft C# .NET 0 6th Oct 2005 09:21 PM
Exception Handling with Shared Constructors =?Utf-8?B?Q2FybFQ=?= Microsoft Dot NET 0 5th Jul 2005 03:29 PM
Custom Exception =?Utf-8?B?U2hhd24gQmVyZw==?= Microsoft Dot NET 8 17th Feb 2005 05:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.