Sunday 28 July 2013

Create a Custom Exception - Information about Exception class



What is an Exception?
In C# Language, Exception handling is done by Try, Catch, Finally to handle the Failure and clean up the resources afterwards. Once a Exception is raised with in a Try statement Then the flow of program control immediately jumps to the associated Exception Handler.

If there is no corresponding exception handler for the Thrown Exception then program stops execution.

How to create a Custom Exception?
          Derive the Class from the Exception class and implement the Constructor and pass the parameter to the base class.

Note:
1.               When the message is doesn’t pass to the base class,Then when your custom Exception is caught in General Exception. When you try to print the message it will print the Exception in following format. “Exception of type ‘CustomException’ was thrown” So base(message) is to be used then only the General exception can knew the message .(or) Catch with the same Exception Type 
public class CustomException:Exception
{
    public CustomException():base()
    { 
    }
    public CustomException(string _message):base(_message)
    {       
    }
    public CustomException(string _message,Exception _innerException):base(_message,_innerException)
    { 
    } 
    public CustomException(SerializationInfo _serinfo,StreamingContext _strcontext):base(_serinfo,_strcontext)
    { 
    }

}

There is an Interesting thing in catch the custom exception, Throw a custom exception with empty message. When you catch that exception with the same type, and print the exception message in console screen , You will notice that it will print a message

public static void Main(string[] args)
    {
        try
        {
            throw new CustomException();
        }
        catch (CustomException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

Exception of type 'CustomException' was thrown.




Why this error message is print, Even though it is catch by same exception type, The Reason is the Message is empty in that Exception when Thrown because of that only this is thrown as That “Exception of type ‘CustomException’ was thrown”.

Some times, some of them thought that Exception is not catch by corresponding Type,so this problem raises. So what they do is they check for the code where they left the catch block for corresponding type.

Now add the Error Message in Custom Exception when it is Thrown

    public static void Main(string[] args)
    {
        try
        {
            throw new CustomException("Custom Error");
        }
        catch (CustomException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

Now the Output is  
Custom Error


Now you can see the Error in Catch instead of type in catch block
From this article I hope that you can learn some of the basic things and some interested things about Exception.