473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Handling Exception

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.

--
Franz Wong
My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com

Nov 15 '05 #1
3 1237
Here is my vision:

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

--
Andrew Gnenny
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Franz" <ho******@yahoo.com.hk> wrote in message
news:un**************@TK2MSFTNGP11.phx.gbl...
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.

--
Franz Wong
My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com

Nov 15 '05 #2
Unless you have an inheritance relation, there is no way to do this.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Franz" <ho******@yahoo.com.hk> wrote in message
news:un**************@TK2MSFTNGP11.phx.gbl...
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.

--
Franz Wong
My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com

Nov 15 '05 #3
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;
}
}

--
Franz Wong
My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com
"Eric Gunnerson [MS]" <er****@online.microsoft.com> ¦b¶l¥ó
news:uq**************@tk2msftngp13.phx.gbl ¤¤¼¶¼g...
Unless you have an inheritance relation, there is no way to do this.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "Franz" <ho******@yahoo.com.hk> wrote in message
news:un**************@TK2MSFTNGP11.phx.gbl...
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.

--
Franz Wong
My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.