Once the custom exception is created, it may be thrown and caught in the same way as any other exception. This includes catching the exact exception or the more generic types that it is derived from. Indeed, one custom exception may derive from another to create a full hierarchy of application exceptions for flexible handling.
The final code sample combines the custom exception with the code for a simple console application. This program throws the custom exception within a try block. The exception message is outputted within the following catch block.
using System;
namespace BlackWasp
{
class TestApp
{
static void Main(string[] args)
{
try
{
// Throw a test exception
throw new InvalidPrinterMarginsException
("The margins are too small");
}
catch (InvalidPrinterMarginsException ex)
{
Console.WriteLine(ex.Message);
}
}
}
// Custom exception class
class InvalidPrinterMarginsException : ApplicationException
{
// Use the default ApplicationException constructors
public InvalidPrinterMarginsException() : base() {}
public InvalidPrinterMarginsException(string s) : base(s) {}
public InvalidPrinterMarginsException(string s, Exception ex)
: base(s, ex) {}
}
}
The .NET Framework provides a rich set of system-defined exception types that can be thrown and caught by the C# developer. However, the list of available exceptions does not cover every eventuality and often it is more appropriate to define custom exceptions for specific error scenarios. This can be achieved by deriving a new exception class from any of the existing types, usually the ApplicationException class.
In the final section of this article we will define a new exception class and demonstrate how it may be thrown and handled. This will be a simple exception with no custom methods or properties. These can be added to the derived class using standard object-oriented programming techniques. However, this is beyond the scope of the C# Fundamentals tutorial and will instead be described in a future object-oriented programming tutorial.
The absolute minimum a new custom exception class needs to have is a name. Let’s say you are designing the login mechanism for a database application and as part of this job you need to create a custom exception which is thrown if a login attempt fails. A good name for such an exception would be LoginFailedException. An absolute minimum implementation in C# then looks like:
public class LoginFailedException: System.Exception
{
}
As you can see, all you need to do to create a basic custom exception is to derive from the Exception class. There’s only one problem with this definition. Since C# unfortunately doesn’t inherit constructors of base classes, this new type only has the standard constructor with no parameters and is therefore relatively useless. So, we need to add at least one constructor which does something useful:
public class LoginFailedException: System.Exception
{
// The default constructor needs to be defined
// explicitly now since it would be gone otherwise.
public LoginFailedException()
{
}
public LoginFailedException(string message): base(message)
{
}
}