Should your switch always have a default? - Peter's Software House

Should your switch always have a default?

I always add a default to my switch statements, even if I am just throwing an exception to warn me of an unexpected value.

switch(value)
{
    case TextAlign.Left:
        //Do something
        break;
    case TextAlign.Right:
        //Do something
        break;
    default:
        thrown new DisNotExpectThatException(value);
}
Published Wednesday, February 18, 2009 3:20 PM by Pieter
Filed under:

Comments

# re: Should your switch always have a default?

Yep, "guilty" of same here. Useful where you have enums that may change in future.

Wednesday, February 18, 2009 8:28 AM by Janco

# re: Should your switch always have a default?

I agree to having defaults and that they could be throwing an exception.

But I think NotSupportedException is good for this.

Tuesday, May 05, 2009 6:02 PM by Erik Juhlin

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: