multiple catch exceptions - MS feature request

  • Thread starter Thread starter Klaus Unterberg
  • Start date Start date
K

Klaus Unterberg

Hello,

how about this:

...
catch (TimeoutException, InvalidCastException, ..)
{
SioComError = true;
}
...

instead of this:

...
catch (TimeoutException)
{
SioComError = true;
}
catch (InvalidOperationException)
{
// sio was closed, because com
// port removed/killed (serial over USB)
SioComError = true;
}
...

These would be nice to have. Thank you!
 
Hello,

how about this:

..
catch (TimeoutException, InvalidCastException, ..)
{
SioComError = true;
}
..

[...]
These would be nice to have. Thank you!

I doubt anyone from Microsoft reads these newsgroups on a regular basis.
Even if they did, I would be surprised if they implemented the syntax
you're asking for.

Note that if you have some common code for different types of exceptions,
you can simplify the implementation a little in one of two ways:

-- Refactor the code into a method shared by multiple catch clauses
-- Have a single catch clause in which you check the type of a base
exception type shared by the exceptions you care about (most often
"Exception")

The former is preferred. The latter can work, but it obviously has the
disadvantage of potentially catching more exceptions than you really want;
you'll have to rethrow the ones that you didn't really want to catch.

In the example you provided, with just a single line of code, either of
these alternatives would be overkill. But for more elaborate situations,
they can be worthwhile to consider.

Pete
 
Back
Top