Saleh
Saleh

Reputation: 3032

try/catch/finally , How to prevent finally when i have return in my catch?

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

Answers (8)

Amit
Amit

Reputation: 13364

the finally doesn't run only in following three conditions :

  1. The System.exit() is called from the try or catch block.
  2. There is an infinite loop inside the try block.
  3. The system crashes or power loss while executing the try 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

NirmalGeo
NirmalGeo

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

Mihai Oprea
Mihai Oprea

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

CloudyMarble
CloudyMarble

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

Programmer
Programmer

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

Øyvind Bråthen
Øyvind Bråthen

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

Nathan
Nathan

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

T.J. Crowder
T.J. Crowder

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

Related Questions