What are Custom Exceptions?
Custom Exceptions are user defined exceptions.
There are exceptions other than the predefined ones which need to be taken care of.
For example: The rules for the minimum balance in a Salary A/C would be different from that in a Savings A/C due to which these things need to be taken care of during the implementation.
Custom exception needs to derive from the System.Exception class. You can either derive directly from it or use an intermediate exception like SystemException or ApplicationException as base class. Custom exceptions are created to handle very specific exceptions and to provide more details about it.
We need custom exceptions to have better predictability of our application. By using custom exceptions we can throw and handle our own exceptions, providing a much more predictable and stable application.
public class MyCustomException : Exception
{
public MyCustomException()
: base()
{
}
public MyCustomException(string Message)
: base(Message)
{
}
public MyCustomException(string Message, Exception InnerException)
: base(Message, InnerException)
{
}
protected MyCustomException(SerializationInfo Info, StreamingContext Context)
: base(Info, Context)
{
}
}