C # try..catch - redirecting error flow from one catch to the next

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); //Note that we have to pass the "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); //Rethrow so that the application-level handler catches and logs it
    }

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; //Rethrow so that the application-level handler catches and logs it
            System.Diagnostics.Debug.Print(
                "We never get here also, but now " + 
                "it obvious and the compiler complains"
            );
            ...!
        }
    }

() , , , try..catch , .

+5
6

, .

catch, "" .

, , " " , , , , . try-catch , try ? , , .

+2

.

try
{
    ...
}
catch (IOException ioEx)
{
    if (sharing violation)
       HandleSharingViolation();
    else 
       HandleNonsharingViolation();
}
catch (Exception ex)
{
       HandleNonsharingViolation();
}

.

catch (Exception ex)
{
     if (ex is IOException && ex.IsSharingViolation()
       HandleSharingViolation();
     else
       HandleNonsharingViolation();
}
+3

, , . .

0

catch catch.

try
{
    try
    {
    }
    catch (IOException ioEx)
    {
        if (....)
        else
           throw;
    }
}
catch
{
}
0

""?

"" IOException, , IOException . , finally, , , .

. .

        bool booleanValue = false;
        try
        {
            test1(); // this would thro IOException

        }
        catch (IOException e)
        {
            booleanValue = true; // whatever you need to do next
        }
        finally
        {
            if (booleanValue)
            {
                Console.WriteLine("Here");
            }
        }
0


{

}

catch ( ioex)
{
  
{

}
catch (Exception ex)
{

}
}

0
source

All Articles