Monday, February 14, 2011

Quick inspection of all Exceptions

To all my code pals,

This post serves to highlight a convenience method that is somewhat useful when one is attempting  examine state of serveral different kinds of Exceptions. The ability to quickly capture and parse all types of Exceptions within a delegate can be quite useful.

I recalled that Objective-C has something called MemberOf (object) . This method allows one to determine whether a given object may be subclassed from a known class. In trying to find a corresponding method in C# I came accross the following paradigm:

typeof(Type).IsAssignableFrom (object.GetType())



if (inner != null)
{
    if (typeof(Exception).IsAssignableFrom(inner.GetType()))
    {
        Exception temp = (Exception)inner;
        Trace.WriteLine(temp.GetType().ToString());
        Trace.WriteLine(temp.Message);
        if (temp.InnerException != null)
            Trace.WriteLine(temp.InnerException.Message);
    }
}

Naturally we could refactor this into fewer lines but the example is just to illustrate the ability to determine base classes in short order. Hopefully this post will help someone avoid digging through the arcane documentation

0 comments:

Welcome to my Circle

It's amazing what one can do in this world. The ability to take the trivial and turn it inside out so that we are seeing not what is or what was, but what might have been. A journey of 1000 steps begins with one.

Light a candle and step into my circle.

燃點蠟燭,並加強到我的圈子

(z) (j)(f)(l)
Loading...