← Back to C# Course | Chapter 10: Exception Handling | Lesson 2 of 6

Exception Types

In this page:

  1. Exception Types

Exception Types

C# has a hierarchy of built-in exception types like ArgumentException, NullReferenceException, and DivideByZeroException, all deriving from System.Exception. Catching a more specific type first lets you handle different failures differently. A catch-all catch (Exception ex) should generally come last.

Note: Order catch blocks from most specific to most general — the compiler enforces this for related types.

Example: Exception Types

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            int a = 10;
            int b = 0;
            Console.WriteLine(a / b);
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine("Division error: " + ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("General error: " + ex.Message);
        }
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.