Click here to Skip to main content
11,636,906 members (73,240 online)
Click here to Skip to main content

Tagged as

Custom exceptions in C#.NET

, 5 Jul 2010 CPOL 99K 22
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.

You may also be interested in...

Comments and Discussions

 
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
memberAshfaque Hussain2-Jun-14 0:05 
GeneralMy vote of 5 Pin
Fred Peters1-Aug-13 6:07
memberFred Peters1-Aug-13 6:07 
AnswerRe: My vote of 5 Pin
Laxman Auti7-Oct-13 7:42
memberLaxman Auti7-Oct-13 7:42 
GeneralIts a good practice to include Severity Level and Log Level ... Pin
ıʇoʞɐlɐʞ ɥsɐʞɐɹd30-Jun-10 4:07
memberıʇoʞɐlɐʞ ɥsɐʞɐɹd30-Jun-10 4:07 
AnswerRe: Its a good practice to include Severity Level and Log Level ... Pin
Laxman Auti7-Oct-13 7:45
memberLaxman Auti7-Oct-13 7:45 

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 | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 5 Jul 2010
Article Copyright 2010 by Laxman Auti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid