473,511 Members | 15,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

catching more than one type of Exception

I have a situation that requires me to catch two different types of
exceptions but do the same processing. Is there a way to do this? In
VB you can stack the Catch statements but what do I do in C#?

For example:

try
{
....
}
catch(IndexOutOfRangeException)
catch(FormatException)
{
....
}
Or something like that.

TIA,
Tom P.
Aug 8 '08 #1
5 1714
Tom P. wrote:
I have a situation that requires me to catch two different types of
exceptions but do the same processing. Is there a way to do this? In
VB you can stack the Catch statements but what do I do in C#?
You can't.

I would argue that this is good, because later the two catch'es
may need to execute different code.

Arne
Aug 8 '08 #2
On Fri, 08 Aug 2008 13:53:58 -0700, Tom P. <pa***********@gmail.comwrote:
I have a situation that requires me to catch two different types of
exceptions but do the same processing. Is there a way to do this? In
VB you can stack the Catch statements but what do I do in C#?

For example:

try
{
...
}
catch(IndexOutOfRangeException)
catch(FormatException)
{
...
}
Or something like that.
You can do:

try
{
// ...
}
catch (Exception exc)
{
if (exc is IndexOutOfRangeException ||
exc is FormatException)
{
// do something
}
else
{
throw;
}
}

or:

void MethodA()
{
try
{
// ...
}
catch (IndexOutOfRangeException)
{
MethodAExceptionHandler();
}
catch (FormatException)
{
MethodAExceptionHandler();
}
}

void MethodAExceptionHandler()
{
// do something
}

But, as Arne says, it may be better to do neither because eventually it
may make sense for the handling to depend on the type of exception.

In fact, the fact that you are trying to handle two different exceptions
in the same way strongly suggests that you are already heading down the
wrong road. If you are handling two different exceptions identically,
then at the very least it's debateable whether you really ought to be
handling each one individually, as opposed to just handling a shared base
class between the two. And it's possible it's a mistake to be handling
the exceptions at all. After all, if you can treat the two disaparate
exceptions identically, just how much real exception recovery could you be
doing at that point?

It's hard to say without knowing more about the rest of the code. But you
definitely should be proceeding carefully.

Pete
Aug 9 '08 #3
On Sat, 09 Aug 2008 12:48:56 -0700, Tom P. <pa***********@gmail.comwrote:
[...] So far I choose to handle both of these
instances the same way, by simply ignoring the string in the renaming
process. I'm not sure yet that I want to ignore EVERY exception, but
so far these two do, in fact, have the same resolution.
But that resolution has little to do with their actual types. You could
just as easily catch the type "Exception". If later you have some other
exception that you want to provide more specific handling for, then you
can include a catch clause specific to that exception (putting it before
the one that catches "Exception", of course).

It might be that in the future, you want to provide the user with better
feedback about why their input is wrong. The user certainly would have a
much better idea of how to fix their input that's not working if they know
whether it's a problem with the format of that input (i.e. entering a
letter when a number was expected) or the meaning of that input (i.e.
entering a number that is itself invalid). And of course, it's also much
easier to fix a problem if the user is actually informed that there's a
problem, but that's a separate issue. :)

But in the meantime, if you're just ignoring the errors, you don't need to
catch a specific exception. Add a specific exception clause if and when
you have something you want to do that actually requires knowing what the
type of the exception is.

Pete
Aug 9 '08 #4
Peter Duniho wrote:
On Sat, 09 Aug 2008 12:48:56 -0700, Tom P. <pa***********@gmail.comwrote:
>[...] So far I choose to handle both of these
instances the same way, by simply ignoring the string in the renaming
process. I'm not sure yet that I want to ignore EVERY exception, but
so far these two do, in fact, have the same resolution.

But that resolution has little to do with their actual types. You could
just as easily catch the type "Exception".
Usually there are some exceptions that you do not want to catch except
at the outermost level. Example: OutOfMemoryException.

Arne
Aug 10 '08 #5
On Sun, 10 Aug 2008 16:45:51 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
Usually there are some exceptions that you do not want to catch except
at the outermost level. Example: OutOfMemoryException.
Well, that depends on what you intend to do with the exception. However,
the OP stated he would just ignore the exception. So what does he care
whether it's OOM or a user input problem? If it's an OOM issue that
matters, he'll get another exception later, and if it's not, then letting
it filter up higher isn't necessary.

Of course, the OP's already stated that he's not actually going to do
anything to handle the exception, but rather will just ignore them, so I
don't see anything wrong with just having two different empty catch
clauses. I'm really just speaking hypothetically at this point with
respect to using a base class in the catch clause.

Pete
Aug 11 '08 #6

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

Similar topics

15
17979
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
8
1721
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
7
2316
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
6087
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
5
1293
by: Andrea | last post by:
....without changing the data type. I have been working on this for over 5 hours, please help me so I don't have to shoot myself. It's due by midnight... Here's my code: (Variables are declared...
3
1790
by: Peter A. Schott | last post by:
I know there's got to be an easy way to do this - I want a way to catch the error text that would normally be shown in an interactive session and put that value into a string I can use later. I've...
2
2337
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
12
18237
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
3
3265
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.