Custom Exception
March 31, 2009
Leave a comment
When you develop a n-tier application, it is quite common to create a custom exception class to inform the upper level application layers from an error.
The followings are the list of the important (in my opinion) things to be considered for creating such a class.
- Do not derive custom exceptions from ApplicationException. For more information refer to Catching and Throwing Standard Exception Types.
- Make the exception serializable.
- Provide at least the following constructors for the exception class:
public class NewException : BaseException, ISerializable
{
public NewException()
{
// Add implementation.
}
public NewException(string message)
{
// Add implementation.
}
public NewException(string message, Exception inner)
{
// Add implementation.
}
// This constructor is needed for serialization.
protected NewException(SerializationInfo info, StreamingContext context)
{
// Add implementation.
}
}
- Designing Custom Exceptions:
http://msdn.microsoft.com/en-us/library/ms229064.aspx - Design Guidelines for Exceptions
http://msdn.microsoft.com/en-us/library/ms229014.aspx