Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip finally in a specific catch

Tags:

c#

try-catch

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);
    }
}
like image 614
Ozkan Avatar asked May 24 '17 10:05

Ozkan


People also ask

When to use finally block instead of TRY CATCH catch?

One could copy the finally code before every return or throw, and wrap catch blocks within their own try/catch to allow for the possibility of an accidental exception occurring, but it's far easier to forgo all that and simply use a finally block.

What happens when a TRY…CATCH…FINALLY statement is nested?

When a Try…Catch…Finally statement is nested in another Try block, Visual Basic first examines each Catch statement in the innermost Try block. If no matching Catch statement is found, the search proceeds to the Catch statements of the outer Try…Catch…Finally block.

What is TRY-CATCH-FINALLY in Java?

Q #4) What is try-catch-finally in Java? Answer: The try-catch-finally block contains the three blocks i.e. try block, catch block, and finally block. Try block contains the code that might throw an exception.

What happens when try catch catch error occurs in PowerShell?

PowerShell then searches for a catch block to handle the error. If the try statement does not have a matching catch block, PowerShell continues to search for an appropriate catch block or Trap statement in the parent scopes. After a catch block is completed or if no appropriate catch block or Trap statement is found, the finally block is run.


2 Answers

Do nothing in the finally block if nothing is to be done

bool skipFinallyFlag = false;
try
{
    //My stuff
}
catch(Exception1 ex1)
{
    //Do something
}
catch(Exception2 ex2)
{
    //Do something
}
catch(Exception3 ex3)
{
    skipFinallyFlag = true;     
}
finally
{
    if(!skipFinallyFlag)
    {
        //Do something
    }
}
like image 139
FortyTwo Avatar answered Sep 23 '22 17:09

FortyTwo


Seems like you could use an exception-filter to filter all exceptions that should have some tidy-up-code and one single exception that doesn´t.

try
{
    DoSomething();
}
catch(Exception e) when (e is MyException || e is AnotherException)
{
    // your error-handling
    // ...
    await Task.Delay(5 * 1000);
}
catch(SpecificExceptionWithoutFinally e)
{
    ...
}

Before C#6 you could also introduce some flag indicating if the code should be executed:

var executeFinally = false;

try
{
    DoSomething();
}
catch(MyException e) 
{
    executeFinally = true;
}
catch(AnotherExceptione)
{
    executeFinally = true;
}    
catch(SpecificExceptionWithoutFinally e)
{
    ...
}
finally
{
    if(executeFinally) {
        await Task.Delay(5 * 1000);
    }
}

Anyway, this seems like a weird requirement, as the whole point of a finally is to be guaranteed to always run regardless on any exception being thrown.

like image 31
MakePeaceGreatAgain Avatar answered Sep 23 '22 17:09

MakePeaceGreatAgain