Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Tip/Trick

Custom exceptions in C#.NET

Rate me:
Please Sign up or sign in to vote.
4.77/5 (31 votes)
5 Jul 2010CPOL 214K   26   7
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:
C#
[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)


Written By
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.

Comments and Discussions

 
QuestionAsd Pin
Aska279028-Apr-18 2:04
Aska279028-Apr-18 2:04 
GeneralMy vote of 5 Pin
QMasters17-Dec-14 23:55
professionalQMasters17-Dec-14 23:55 
GeneralMy vote of 5 Pin
Ashfaque Hussain2-Jun-14 0:05
Ashfaque Hussain2-Jun-14 0:05 
GeneralMy vote of 5 Pin
Fred Peters1-Aug-13 6:07
Fred Peters1-Aug-13 6:07 
AnswerRe: My vote of 5 Pin
Laxman Auti7-Oct-13 7:42
Laxman Auti7-Oct-13 7:42 
GeneralIts a good practice to include Severity Level and Log Level ... Pin
PSK_30-Jun-10 4:07
PSK_30-Jun-10 4:07 
AnswerRe: Its a good practice to include Severity Level and Log Level ... Pin
Laxman Auti7-Oct-13 7:45
Laxman Auti7-Oct-13 7:45 

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

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