Handling Exception

  • Thread starter Thread starter Franz
  • Start date Start date
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.
 
Here is my vision:

try
{
}
catch(Exception E)
{
if ((E is ExceptionA) || (E is ExceptionB))
{
XXXXXXXXXXXXX
}
else
{
YYYYYYYYYYYYY
}
}
 
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;
}
}
 
Back
Top