Reputation: 3032
I have this code and i want to prevent finally execution
try
{
throw new Exception("Try Error!!!");
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
Console.ReadLine();
if(exp.Message == "Try Error!!!")
return;
}
finally
{
Console.WriteLine("Finally...");
Console.ReadLine();
}
But return doesn't work.
Upvotes: 2
Views: 5034
Reputation: 13364
the finally doesn't run only in following three conditions :
try
or catch
block.for all other conditions .... the finally is always executed... having a boolean variable to control the execution of finally code is a clever approach... go for it...
Upvotes: 12
Reputation: 771
System.exit() can be used to avoid the execution of the finally block Finally Block. But if your condition is that the execution should not be hindered, then please do reframe the question.
Upvotes: 1
Reputation: 2047
Finally gets executed no matter what happens in the try catch block. It's meant that way so that you can free up resources before you exit the function.
Upvotes: 1
Reputation: 37566
Why would you want to do that?
Finally is there so it allways runs and does some finishing work which has to be done anyway, if ou want the code to be executed only when the try runs then write in in the try statement, otherwise if only when it fails then in the catch one. Code that has to tun in both cases has to be written in the finally section.
Upvotes: 3
Reputation: 6753
But your return is in a if clause so it will not always work. Try putting it outside the if in catch
Upvotes: -1
Reputation: 60694
The whole point of finally is that it runs regardless of how the try/catch block executed. One workaround can be something like this:
bool runfinallycode = true;
try
{
throw new Exception("Try Error!!!");
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
Console.ReadLine();
if(exp.Message == "Try Error!!!")
runfinallycode = false;
}
finally
{
if( runfinallycode )
{
Console.WriteLine("Finally...");
Console.ReadLine();
}
}
Upvotes: 2
Reputation: 6216
That's the point of finally - it always runs.
try this:
bool runfinally = true;
try {
throw new Exception("test");
} catch {
runfinally = false;
} finally {
if(runfinally) {
// do stuff.
}
}
Upvotes: 9
Reputation: 1074295
The only way is to set a flag variable that your finally
code tests, e.g.:
bool handled = false;
try
{
throw new Exception("Try Error!!!");
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
Console.ReadLine();
if (exp.Message == "Try Error!!!")
{
handled = true;
return; // This isn't really necessary unless you have code below it you're not showing
}
}
finally
{
if (!handled)
{
Console.WriteLine("Finally...");
Console.ReadLine();
}
}
...but I'd really consider revisiting your logic so you don't need to do that. The whole point of finally
is that no matter what happens within the try
block, that finally
code will run, whether you return early from the code in the try
block, or an exception occurs, or you return from within the catch
block.
Upvotes: 2