Skip finally in a specific catch

Viewed 2589

Let's say I have to catch 3 different exceptions, so I write 3 separate catch blocks. But I want to skip the finally block for one specific exception.

As far as I know this is not possible using a builtin flag. But can you advise how to solve this coding problem in elegant way?

When not using finally, I have to rewrite the same code several times, in try and also in other catch blocks.

More information: In finally I let the thread sleep for some time (await Task.Delay(5 * 1000);) But if I receive a OperationCanceledExceptionthen I don't want the finally to be run. I want it to break as fast as possible.

while (true)
{
    try
    {
        _cts.Token.ThrowIfCancellationRequested();
    }
    catch (OperationCanceledException)
    {
        break;
    }
    catch (CustomException1 e)
    {

    }
    catch (CustomException2 e)
    {

    }                
    finally
    {
        await Task.Delay(5 * 1000);
    }
}
2 Answers
Related