#178 – Throwing an Exception from a switch Statement
December 12, 2010 2 Comments
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; }
Some of your examples are hilarious. Keep up the good work !
Thanks!