Sometimes, you'll encounter a situation where you might think that the predefined exceptions do not satisfy your condition. In this instance, you might wish there was a way to create your own exception classes and use them. Thankfully, in C#, there is actually a mechanism where you can create your own custom exceptions, and can write whatever message is appropriate for that kind of exception. Let's look at an example of how to create and use custom exceptions:
using System;
namespace ExceptionCode
{
class HelloException : Exception
{
public HelloException() { }
public HelloException(string message) : base(message) { }
public HelloException(string message, Exception inner) : base(message, inner) { }
}
class Program
{
static void Main(string[] args)
{
try
{
throw new HelloException("Hello is an exception!");
}
catch (HelloException ex)
...