All exceptions are derived from the System.Exception class in .NET Framework. So, in a scenario where these predefined exceptions don't suit our requirements, the framework allows us to create our own exceptions by deriving our exception class from the Exception class.
In the following example, we are creating a custom exception and inheriting from the Exception class. We can use different constructors for this:
public class MyCustomException : Exception
{
public MyCustomException():base("This is my custom exception")
{
}
public MyCustomException(string message)
: base($"This is from the method : {message}")
{
}
public MyCustomException(string message, Exception innerException)
: base($"Message: {message}, InnerException: {innerException}")
{
}
}
When you create your own exception...