I have a try..catch block that looks like this:
try
{
...
}
catch (IOException ioEx)
{
...
}
catch (Exception ex)
{
...
}
I would like to handle only a certain type IOException, namely access violation (Win32 0x20). Other IOExceptions and all other Exceptiondescendants should be handled, usually by a second catch-all catch.
As soon as I know that an IOException is not a sharing violation, how can I purely redirect the error flow to a common one catch? If I go back in catch (IOException), the second catch does not call. I know I can nest try..catches, but is there a cleaner way?
EDIT: in the logic of the factoring handler
, , , , , , .
, catch . "" , . , , .
, . , , # , Resharper, :
private void Foo()
{
string a = null;
try
{
a = Path.GetDirectoryName(a);
System.Diagnostics.Debug.Print(a);
}
catch (Exception ex)
{
HandleException(ex, a);
System.Diagnostics.Debug.Print(
"We never get here and it not obvious" +
"until you read and understand HandleException"
);
...!
}
}
static void HandleException(Exception ex, string a)
{
if (a != null)
System.Diagnostics.Debug.Print("[a] was not null");
throw (ex);
}
VS
private void Bar()
{
string a = null;
try
{
a = System.IO.Path.GetDirectoryName(a);
System.Diagnostics.Debug.Print(a);
}
catch (Exception ex)
{
if (a != null)
System.Diagnostics.Debug.Print("[a] was not null");
throw;
System.Diagnostics.Debug.Print(
"We never get here also, but now " +
"it obvious and the compiler complains"
);
...!
}
}
() , , , try..catch , .