Custom exceptions
Learn to write your own Exception-derived class.
Defining a custom exception class
Sometimes you want to define your own exception class to represent errors specific to your application's domain. This is done by deriving from the Exception class. The naming convention is to end the class name with Exception, so its purpose is immediately clear.
For example, class InvalidAgeException : Exception { ... } declares a new type that represents age-related errors. You add a constructor that calls base(msg) to set the Message property of the base class. It then works with the standard exception machinery.
Deriving from Exception
When writing a custom exception class, you derive with the form : Exception. The class has at least one constructor that takes a string msg parameter and passes it to the base with : base(msg). The Message property is then set automatically.
For example, public InvalidAgeException(string msg) : base(msg) { } creates a new InvalidAgeException with the given message. You can then throw it with throw new InvalidAgeException("Negative age"); and catch it with catch (InvalidAgeException ex) for a specific type.
When to use a custom exception?
Custom exception classes are used to represent errors specific to your application's domain. For example, in a library app BookNotFoundException, in a payment system PaymentFailedException make sense. This makes catch blocks more readable and meaningful.
Using a custom type instead of the standard Exception lets you distinguish the source of the error at the type level. Still, defining a new class for every small error creates clutter; use it only when you truly need domain-specific handling.
Exercise Define and throw a custom exception
At the top of the file, define the InvalidAgeException class; derive from Exception and add the constructor public InvalidAgeException(string msg) : base(msg) { }. In Main, inside a try block, throw throw new InvalidAgeException("Invalid age"); when if (age < 0). In catch (InvalidAgeException ex), write Console.WriteLine(ex.Message);.
Show example solution
Exception { public InvalidAgeException(string msg) : base(msg) { } }
In try, add if (age < 0) throw new InvalidAgeException("Invalid age");
Example solution:
using System;
class InvalidAgeException : Exception
{
public InvalidAgeException(string msg) : base(msg) { }
}
class Program
{
static void Main()
{
int age = -5;
try
{
if (age < 0)
{
throw new InvalidAgeException("Invalid age");
}
}
catch (InvalidAgeException ex)
{
Console.WriteLine(ex.Message);
}
}
}
Test yourself
-
What does base(msg) do?
class MyException : Exception { public MyException(string msg) : base(msg) { } }- Resets Message
- Throws a new error
- Passes Message to the base class
- Clears StackTrace
Show answer
Passes Message to the base class — Correct! base(msg) calls the base class constructor and sets Message.
-
Class declaration
class InvalidAgeException : ____ { public InvalidAgeException(string msg) : base(msg) { } }Show answer
class InvalidAgeException : Exception { public InvalidAgeException(string msg) : base(msg) { } } -
Match each element with its description
- : Exception
- Derived class
- base(msg)
- Base constructor
- throw new InvalidAgeException
- Throwing