Click here to Skip to main content
13,450,958 members (52,820 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 5 Jul 2010

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:
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) { }


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.


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


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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180318.3 | Last Updated 5 Jul 2010
Article Copyright 2010 by Laxman Auti
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid