By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,464 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,464 IT Pros & Developers. It's quick & easy.

Exception handling for switch statement

P: n/a
Hi,

I have a switch statement that has 5+ case statements. Each of these case
statements copies form one array to another. Rather than doing a separate
try..catch statement for each case statement I wanted to wrap all the case
statements in one try catch statement.

However I am having problems doing this as there is a compilation error
wrapping all the case statements inside the try block or putting the whole
switch statement in the try block.

Does anyone know how I can use one try catch staments for my switch statement?

Thanks In Advance
Macca
Mar 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Should be able to:

try
{
switch(myValue)
{
case possibleValue1:
myMethod1();
break;
case aDifferentValue:
if (myFlags & 0x8 != -1)
myOtherMethod(myFlags ~0x8);
break;
}
}
catch(Exception e)
{
//handle your exception
}

"Macca" wrote:
Hi,

I have a switch statement that has 5+ case statements. Each of these case
statements copies form one array to another. Rather than doing a separate
try..catch statement for each case statement I wanted to wrap all the case
statements in one try catch statement.

However I am having problems doing this as there is a compilation error
wrapping all the case statements inside the try block or putting the whole
switch statement in the try block.

Does anyone know how I can use one try catch staments for my switch statement?

Thanks In Advance
Macca

Mar 29 '06 #2

P: n/a
Macca <Ma***@discussions.microsoft.com> wrote:
I have a switch statement that has 5+ case statements. Each of these case
statements copies form one array to another. Rather than doing a separate
try..catch statement for each case statement I wanted to wrap all the case
statements in one try catch statement.

However I am having problems doing this as there is a compilation error
wrapping all the case statements inside the try block or putting the whole
switch statement in the try block.

Does anyone know how I can use one try catch staments for my switch statement?


It would help if you'd say what compilation error you're getting.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.