#178 – Throwing an Exception from a switch Statement
December 12, 2010 Leave a Comment
Instead of a break statement as a terminator of a case clause in a switch statement, you can also throw an exception.
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;
}