#178 – 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.

            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;
            }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #178 – Throwing an Exception from a switch Statement

  1. Dustin says:

    Some of your examples are hilarious. Keep up the good work !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: