Throwing an Exception from a switch Statement

Instead of a break statement as a terminator of a case clause in a switch statement, you can also throw an exception.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (surname)       // surname is a string
{
    case "Aarhus":
    case "Enevoldsen":
        Console.WriteLine("Norwegian");
        break;
    case "Brosnan":
    case "McGill":
        Console.WriteLine("Irish");
        throw new ApplicationException("Software doesn't work for Irish people");
    default:
        Console.WriteLine("UNKNOWN origin");
        break;
}