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

Tagged as

Custom exceptions in C#.NET

, 5 Jul 2010 CPOL
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.
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 5 PinprofessionalQMasters18-Dec-14 0:55 
GeneralMy vote of 5 PinmemberAshfaque Hussain2-Jun-14 1:05 
Nice explanation...
GeneralMessage Removed PinmemberAshfaque Hussain2-Jun-14 0:50 
GeneralMy vote of 5 PinmemberFred Peters1-Aug-13 7:07 
AnswerRe: My vote of 5 PinmemberLaxman Auti7-Oct-13 8:42 
GeneralIts a good practice to include Severity Level and Log Level ... Pinmemberıʇoʞɐlɐʞ ɥsɐʞɐɹd30-Jun-10 5:07 
AnswerRe: Its a good practice to include Severity Level and Log Level ... PinmemberLaxman Auti7-Oct-13 8: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
Web03 | 2.8.150327.1 | Last Updated 5 Jul 2010
Article Copyright 2010 by Laxman Auti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid