refines the System.Exception base class with name of the type and time of exception to create your own Exception

image_pdfimage_print
   
 

using System;

public class ConstructorException : Exception {

    public ConstructorException(object origin)
        : this(origin, null) {
    }

    public ConstructorException(object origin, Exception innerException)
        : base("Exception in constructor", innerException) {
        prop_Typename = origin.GetType().Name;
        prop_Time = DateTime.Now.ToLongDateString() + " " +
            DateTime.Now.ToShortTimeString();
    }

    protected string prop_Typename = null;
    public string Typename {
        get {
            return prop_Typename;
        }
    }

    protected string prop_Time = null;
    public string Time {
        get {
            return prop_Time;
        }
    }
}
public class Starter {
    public static void Main() {
        try {
            MyClass obj = new MyClass();
        } catch (ConstructorException except) {
            Console.WriteLine(except.Message);
            Console.WriteLine("Typename: " + except.Typename);
            Console.WriteLine("Occured: " + except.Time);
        }
    }
}

class MyClass {
    public MyClass() {
        // initialization fails
        throw new ConstructorException(this);
    }
}