0
0
CsharpConceptBeginner · 3 min read

Exception Hierarchy in C#: What It Is and How It Works

In C#, the exception hierarchy is a structured way of organizing error types where all exceptions inherit from the base class System.Exception. This hierarchy allows you to catch specific or general errors by handling exceptions at different levels in the inheritance tree.
⚙️

How It Works

Think of the exception hierarchy in C# like a family tree for errors. At the top is the System.Exception class, which is the parent of all error types. Just like a family has different members with shared traits, exceptions inherit common features from this base class.

This setup helps your program understand and organize errors. For example, some exceptions are very general, like Exception itself, while others are more specific, like ArgumentNullException which means a method got a null argument it didn't expect.

When your program runs into a problem, it throws an exception object from this hierarchy. You can then write code to catch either broad exceptions or very specific ones, depending on how precise you want your error handling to be.

💻

Example

This example shows how different exceptions in the hierarchy can be caught separately or generally.

csharp
using System;

class Program
{
    static void Main()
    {
        try
        {
            ThrowArgumentNull();
        }
        catch (ArgumentNullException ex)
        {
            Console.WriteLine("Caught specific exception: " + ex.GetType().Name);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Caught general exception: " + ex.GetType().Name);
        }
    }

    static void ThrowArgumentNull()
    {
        throw new ArgumentNullException("param", "Parameter cannot be null.");
    }
}
Output
Caught specific exception: ArgumentNullException
🎯

When to Use

Use the exception hierarchy to write clear and effective error handling in your programs. Catch specific exceptions when you want to handle certain errors differently, like fixing a missing file or invalid input. Catch general exceptions when you want a fallback for unexpected errors.

For example, in a file-reading program, you might catch FileNotFoundException to tell the user the file is missing, but also catch Exception to log any other unexpected problems.

This approach helps keep your program stable and user-friendly by responding appropriately to different error situations.

Key Points

  • All exceptions in C# inherit from System.Exception.
  • Specific exceptions inherit from more general ones, forming a hierarchy.
  • You can catch exceptions at any level to control error handling granularity.
  • Using the hierarchy helps write robust and maintainable error handling code.

Key Takeaways

All C# exceptions derive from the base class System.Exception.
The exception hierarchy lets you catch errors specifically or generally.
Use specific exception types to handle known error cases precisely.
Catch general exceptions as a fallback for unexpected errors.
Understanding the hierarchy improves your program's error handling.