Can/Should you throw exceptions in a c# switch statement?

Posted by Kettenbach on Stack Overflow See other posts from Stack Overflow or by Kettenbach
Published on 2010-04-22T18:13:44Z Indexed on 2010/04/22 18:23 UTC
Read the original article Hit count: 309

Filed under:
|
|
|

Hi All, I have an insert query that returns an int. Based on that int I may wish to throw an exception. Is this appropriate to do within a switch statement?

 switch (result)
        {

            case D_USER_NOT_FOUND:
                throw new ClientException(string.Format("D User Name: {0} , was not found.", dTbx.Text));
            case C_USER_NOT_FOUND:
                throw new ClientException(string.Format("C User Name: {0} , was not found.", cTbx.Text));
            case D_USER_ALREADY_MAPPED:
                throw new ClientException(string.Format("D User Name: {0} , is already mapped.", dTbx.Text));
            case C_USER_ALREADY_MAPPED:
                throw new ClientException(string.Format("C User Name: {0} , is already mapped.", cTbx.Text));
            default:

                break;
        }

I normally add break statements to switches but they will not be hit. Is this a bad design? Please share any opinions/suggestions with me.

Thanks, ~ck in San Diego

© Stack Overflow or respective owner

Related posts about c#

Related posts about switch-statement