Handling Exception

F

Franz

try
{
}
catch (ExceptionA A)
{
XXXXXXXXXXXXX
}
catch (ExceptionB B)
{
XXXXXXXXXXXXX
}
catch (Exception)
{
YYYYYYYYYYYYY
}

Actually the codes of hanlding both Exception A and B are same. I want to
put it in a single catch block. Although I know that I can group the code
into a function and call it in these two catch blocks, I don't want to have
duplicated code. Also I don't want to give a inheritance relationship for
both ExceptionA and ExceptionB. I

Is there anything like the following? I want to do the following because if
a large number of exception use the same hanlding code, then I can group
them into a small number of brackets.

try
{
}
catch (ExceptionA A)
catch (ExceptionB B)
{
XXXXXXXXXXXXX
}
catch (Exception)
{
YYYYYYYYYYYYY
}

Thanks.
 
A

Andrew Gnenny

Here is my vision:

try
{
}
catch(Exception E)
{
if ((E is ExceptionA) || (E is ExceptionB))
{
XXXXXXXXXXXXX
}
else
{
YYYYYYYYYYYYY
}
}
 
F

Franz

Yes, you are right.
Finally, I accept the following solution.

catch (Exception e)
{
switch (e.GetType().ToString())
{
case "ExceptionA":
case "ExceptionB":
Console.WriteLine("This is Exception A and B");
break;
}
}
 

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

Top