Exception handling for switch statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top