Click here to Skip to main content
Click here to Skip to main content

Tagged as

Custom exceptions in C#.NET

, 5 Jul 2010
Rate this:
Please Sign up or sign in to vote.
During development, I always look for an optimal and efficient way to achieve a goal. Last time, I came with the way to log the exceptions along with method parameters for which exception has thrown.
 
Generally, for large scale projects (i.e. having multiple modules talks to each other), one should define custom exceptions.
 
Define custom exception class:
[Serializable]
public class CustomException : Exception
{
    public CustomException()
        : base() { }
    
    public CustomException(string message)
        : base(message) { }
    
    public CustomException(string format, params object[] args)
        : base(string.Format(format, args)) { }
    
    public CustomException(string message, Exception innerException)
        : base(message, innerException) { }
    
    public CustomException(string format, Exception innerException, params object[] args)
        : base(string.Format(format, args), innerException) { }
    
    protected CustomException(SerializationInfo info, StreamingContext context)
        : base(info, context) { }
}
Example:
 
1. Throw exception with out message
throw new CustomException()
2. Throw exception with simple message
throw new CustomException(message)
3. Throw exception with message format and parameters
throw new CustomException("Exception with parameter value '{0}'", param)
4. Throw exception with simple message and inner exception
throw new CustomException(message, innerException)
5. Throw exception with message format and inner exception. Note that, the variable length params are always floating.
throw new CustomException("Exception with parameter value '{0}'", innerException, param)
6. The last flavor of custom exception constructor is used during exception serialization/deserialization.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Laxman Auti
Software Developer (Senior) Varian Medical Systems
India India
He is working on Microsoft Technologies since 5 years and loves to working on it. He has been contributing in development of various products in Varian Medical Systems whose motto to throw away cancer from the world by providing effective and efficient hardware and software solutions.
 
He has great knowledge of design and development in C#.NET. Another thing is that he loves to help people who need it at any cost.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAshfaque Hussain2-Jun-14 0:05 
GeneralMessage Removed PinmemberAshfaque Hussain1-Jun-14 23:50 
GeneralMy vote of 5 PinmemberFred Peters1-Aug-13 6:07 
AnswerRe: My vote of 5 PinmemberLaxman Auti7-Oct-13 7:42 
GeneralIts a good practice to include Severity Level and Log Level ... Pinmemberıʇoʞɐlɐʞ ɥsɐʞɐɹd30-Jun-10 4:07 
AnswerRe: Its a good practice to include Severity Level and Log Level ... PinmemberLaxman Auti7-Oct-13 7:45 
Absolutely! we should consider the things which you will needed for identifying a issues while defining such custom exception classes.
Knock out 't' from can't, you can if you think you can. Cool | :cool:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 5 Jul 2010
Article Copyright 2010 by Laxman Auti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid