We help IT Professionals succeed at work.

Advanced Exception Handling

mrichmon asked
Medium Priority
Last Modified: 2010-04-16
I can't do this:

class MyCustomInitializationException : TypeInitializationException

because TypeInitializationException is sealed.

The reason I wanted to tdo this is that we really want the .TypeName property in our custom exception, along with other info.

How can I manually create this property since I cannot inherit from the TypeInitializationException?
Watch Question


A hack I have is to pass in the type to the constructor of the custom exception, but that seems ugly.  Or is that how the TypeInitializationException is actually done?

(Tried using a reflector, but couldn't quite see it, but am not experienced with looking at that)

Not super for performance so make sure isnt thrown a lot but easiest way is maybe this...

class CustomException {
CustomException(exp.val, exp.val, exp.val, val, val, val);
  //custom properties here

  //properties from TypeInitializationException here

try {

catch(TypeInitializationException exp)
   throw new CustomException(exp.val, exp.val, exp.val, val, val, val);


instead of trying to inheritate from it, just use composition.

class CustomException {
   private TypeInitializationException typeExp = new TypeInitializationException ();

   //propeties to it here

   //custom stuff here.


i showed both and pretty much the only ways to do it because im not sure if you are working with an exception that isnt thrown by you (then use the first) or if you throwing it yourself, use latter.

your are pretty much on track imo
Most Valuable Expert 2012
Top Expert 2008
Here is another way:

public class MyCustomInitializationException : Exception

    public MyCustomInitializationException(string typeName)
      _typeName = typeName;

    private string _typeName = "";

    public string TypeName
      get { return _typeName; }


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts



This is what I am already doing.  (Notice I said that I could just pass the typeName in through the constructor as my hack).


I think that including an exception as a private member inside my exception (your second one) just to get that one field is a worse hack than sending the field into the constructor.  And the first example, is really also just sending stuff in through the constructor.

For the record, the code I have so far, which I feel is a hack is this:

public class CustomInitializationException: Exception
      // private members that are added on to the base class
      private string typeName;
      private string customField;

      // Base exception only initializes the typeName and the error message
      public CustomInitializationException(string typeName, string customField, string message) : base("Custom Initialization failed: " + message)
            this.typeName = typeName;
            this.customField = customField;

      // Exception that contains an inner exception as well as a message
      public CustomInitializationException(string typeName, string customField, string message, Exception innerException) : base("Custom Initialization failed: " + message, innerException)
            this.typeName = typeName;
            this.customField = customField;

      // more constructors or whatever else goes here....

      public string TypeName { get { return this.typeName; } }
      public string CustomField { get { return this.customField; } }

Um you might want to rethink things.  You keep referring to everything as a hack but what on earth are you looking for?  By the way, using a class inside of a nother is an actual concept called composition.  Its a programming paradigm just like inheritence is and provides a great alternative to it.  All your doing is duplicating code instead of composing the same thing from existing.

You either compose the class you need or inherit from in which you implement/override what works for you.  That is it and really not much more to it.

MySuperSpecialDataSet {

     private DataSet _dataset = new DataSet();


You would not rewrite the entire dataset class just to incorporate its functioanlity int your class, would you?


No I would not.

But I also would not include an exception as an inner type just to get one string field.  That is not the idea behind composition.

It is not a "great" alternative to inheritance.  It is a different mechanism used for different situations.  Inheritance iw when you want existing functionality, but want to expand it.  That is what I want in this case.

I assumed - and maybe this is incorrect - that the TypeInitializationException, got the type using some mechanism other than manually passing in.  If not then what I have is already the best solution and not a hack, but somehow I doubt that.  Since I noticed that there were constructors for TypeInitializationException that did not take in a string of the typename.


I cant and didnt assume you where only using it for one string field.  I've never even looked at its interface.  TypeInitializationException could only got its type by explicityly telling it so.  If you looked at the IL youd be able to see that microsoft is calling it and assigning properties the same way you would.

Composition it IS absolutely an alternative and most publications i've read refer to it as the same.

"Inheritance iw when you want existing functionality, but want to expand it." ummm, and composition doesnt allow the same!!!???

-Notice the name of the article, read on is good stuff.

**Component-oriented design as an alternative to inheritance**


An alternative way to design the aforementioned system of persons, students, and employees would be to define helper classes Transcript and Job to store the additional information for a student and employee, respectively. Then, each Person object can contain a collection of Transcript objects and a collection of Job objects. This removes the aforementioned constraints:


Well I disagree about composition.  I could point out articles supporting my point of view as well - and not just froma wiki which is information that can be accurate or totally incorrect.  But instead let's just agree to have different opinions on that since it isn't really the point of this question.

>>I cant and didnt assume you where only using it for one string field.  I've never even looked at its interface
Then I respectfully suggest that you shouldn't have commented until doing the research to see that the only difference here IS the one string field that it adds.  It helps to do a little research before making suggestions - just as Ido research before posting a question, or posting suggestions to others.

>>If you looked at the IL
I don't know what IL stands for.  I did try tools that unbuild the dll and show you the code, and noticed that only some constructors took in a string - others did not.  BUt they all offer that field.  So that is what I was wondering how it was done.
your right, by the way i found some interesting info on how it works internally and should help you out.

so basically it uses a catch and then re-throws an exception.

Exceptions in class initializers? If an exception occurs in a class initializer .cctor,
then the class shall be marked as being in a specific erroneous state and the specific exception
TypeInitializationException is thrown. This means that an exception can
and will escape the body of a .cctor only by a TypeInitializationException.

Any further attempt to access the corresponding class in the current application domain
will throw the same TypeInitializationException object. This detail is not
specified by the ECMA standard but it seems to correspond to the actual CLR implementation
and it complies with the related specification for C] in the ECMA standard
(see Test 7 in [12]).

Therefore, we assume that the code sequence of every .cctor
is embedded into a catch handler. This catch handler catches exceptions of type
Object, i.e., any exception, occured in .cctor, discards it, creates an object of type
TypeInitializationException and throws the new exception.

source: http://www.di.unipi.it/~boerger/Papers/CSharp/ClrExcJOT06.pdf



very interesting :o)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.