Catching lots of exceptions

P

Peter

Hi

is there some "nice" way of catching lots of different exceptions in
one catch block?

Something like:

try
{
.... do stuff which could throw lots of different exceptions ...
}
catch (IOException, BadImageFormatException,
UnauthorizedAccessException ex)
{
.... common handling for the exceptions
}

Usually I have to have lots of separate catch blocks.

Thanks,
Peter
 
A

Anthony Jones

Peter said:
Hi

is there some "nice" way of catching lots of different exceptions in
one catch block?

Something like:

try
{
... do stuff which could throw lots of different exceptions ...
}
catch (IOException, BadImageFormatException,
UnauthorizedAccessException ex)
{
... common handling for the exceptions
}

Usually I have to have lots of separate catch blocks.

Since all exceptions derive from System.Exception you can do:-
try { ... }
catch (Exception err)
{
//Handle all exceptions.
}
 
P

Peter

Anthony said:
Since all exceptions derive from System.Exception you can do:-
try { ... }
catch (Exception err)
{
//Handle all exceptions.
}

Thanks - I realised that - what I maybe didn't express clearly was that
I want to catch a number of specific exceptions and perform the same
handling on them. Other exceptions I don't want to handle at all.

I guess I could do something like:

catch (Exception ex)
{
if (ex is IOException || ex is BadImageException ||
ex is UnauthorizedAccessException)
{
... do common handling ...
}
else
{
throw;
}
}


/Peter
 
A

Anthony Jones

Peter said:
Thanks - I realised that - what I maybe didn't express clearly was that
I want to catch a number of specific exceptions and perform the same
handling on them. Other exceptions I don't want to handle at all.

I guess I could do something like:

catch (Exception ex)
{
if (ex is IOException || ex is BadImageException ||
ex is UnauthorizedAccessException)
{
... do common handling ...
}
else
{
throw;
}
}

Place your common handling in its own function (or you might even use an
anonymous function). Then use a specific catch section for each exception
and in each call the common handling function.
 
P

Pavel Minaev

Thanks - I realised that - what I maybe didn't express clearly was that
I want to catch a number of specific exceptions and perform the same
handling on them. Other exceptions I don't want to handle at all.

I guess I could do something like:

catch (Exception ex)
{
  if (ex is IOException || ex is BadImageException ||
      ex is UnauthorizedAccessException)
  {
    ... do common handling ...
  }
  else
  {
    throw;
  }

}

You can, but it may bite you during debugging: VS debugger breaks by
default on uncaught exception, but not on caught ones - as this block
catches everything, if something goes wrong, you'll find the debugger
break at rethrow, and not where the exception originally came from.
This can be quite annoying.

Unfortunately, C# does not have union types (A or B), neither in
general, nor specifically in catch blocks. You may want to file a
suggestion at https://connect.microsoft.com/VisualStudio/Feedback -
but I wouldn't expect that anytime sooner than C# 5.0 ;)
 
P

Peter Morris

try
{
}
catch {IOException}
{
DoCommonThing();
}
catch {BadImageFormatException}
{
DoCommonThing();
}
catch {UnauthorizedAccessException }
{
DoCommonThing();
}


Personally I hate it and I wish it would change. I spoke to the guy who
writes the Oxygene compiler (Prism, Chrome, or whatever it's called these
days) for RemObjects and moaned to him about it, and so he wrote a feature
into his compiler. Not that it did me any good because I don't use his .NET
language, but the compiler just made an anonymous method and called it

try
blah
except
on IOException, BadImageFormatException, UnauthorizedAccessException do
begin
code 1
code 2
code 3
end;
end;


converts to

try
{
}
catch (IOException)
{
anonMethod1;
}
catch (BadImageFormatException)
{
anonMethod1;
}
catch (UnauthorizedAccessException )
{
anonMethod1;
}

private void anonMethod1()
{
code 1
code 2
code 3
}
 
P

Pavel Minaev

Personally I hate it and I wish it would change.  I spoke to the guy who
writes the Oxygene compiler (Prism, Chrome, or whatever it's called these
days) for RemObjects and moaned to him about it, and so he wrote a feature
into his compiler.

It's Delphi Prism now.

But yes, one thing I always liked about it was the aggressive feature
coverage. And I mean stuff like "for parallel" working via PLINQ, auto-
properties with notification, etc. I fully expect to see DbC move
to .NET 4.0 contract libraries by the time the beta is out :)

Well, what can I say? Just keep poking MS, Connect is the best place
to do that, as usual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top