473,241 Members | 1,549 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,241 software developers and data experts.

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 1710
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
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
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
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
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
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
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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...

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.