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 OperationCanceledException
then 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);
}
}
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.
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.
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.
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.
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
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With