locked
using finally block RRS feed

  • Question

  • hi

    i've used try/catch/finally blocks in my application and i want the flow of program not to enter the finally block if it has entered the catch. i may be wrong but i think normally this should happen; however, it doesn't! how can i overcome the problem?

    thanks
    Sunday, July 26, 2009 5:43 AM

Answers

  • like what?

    Typically a return:

        bool DoStuff()
        {
          try
          {
            //Statement to test
          }
          catch
          {
            //Clean up after error
            return false;
          }
          //Process success
          return true;
        }
    Sunday, July 26, 2009 9:14 AM
  • Just don't use the finally block.  Move the statements its got to the bottom of the try block:

      try {
        // original statements
        //...
        try {
          // statements in old finally block
          //...
        }
        catch { throw; }
      }
      catch {...}
        // original catch statements
      }

    Hans Passant.
    Sunday, July 26, 2009 9:34 AM

All replies

  • Here is the flow of try-catch-finally:


    // Code block 1
    try {
        // Code block 2
    } catch (Exception e) {
        // Code block 3
    } finally {
        // Code block 4
    }
    // Code Block 5


    If there is no problem in the try, then Code blocks 1, 2, 4, 5 execute. If there is a problem in the try, then Code blocks 1, 2*, 3, 4, 5 execute. The finally block always executes. What it sound like to me is that you want Blocks 1, 2*, 3 to execute on error, and 1, 2, 5 to execute on no error. The easiest way I can think of is to set a boolean on whether block 5 should run. Default it to true, set it to false in the catch block


    * Code block 2 may not execute fully, depending on where the unhandled exception occurs.
    Ron Whittle - If the post is helpful or answers your question, please mark it as such.
    Sunday, July 26, 2009 6:50 AM
  • you mean i should surround the finally block in an if block whose condition depends on a value which has been set in a catch block???
    Sunday, July 26, 2009 7:51 AM
  • Don't use the finally.  Break out of the Catch to a statement that bypasses the code that would run if the Catch isn't entered. 
    Sunday, July 26, 2009 8:08 AM
  • like what?
    Sunday, July 26, 2009 8:39 AM
  • Hello,

    Hmm I think that this defeats the the whole point of having a finally block, it's there to let you finish the process/work safely, special when there's a catch! so like 'JohnWein' said don't use finally at all, you can use a boolean value to indicate a try, if there's a try set it to true, otherwise it's a catch, do your thing.
    Eyal, Regards.
    Sunday, July 26, 2009 8:49 AM
  • like what?

    Typically a return:

        bool DoStuff()
        {
          try
          {
            //Statement to test
          }
          catch
          {
            //Clean up after error
            return false;
          }
          //Process success
          return true;
        }
    Sunday, July 26, 2009 9:14 AM
  • Just don't use the finally block.  Move the statements its got to the bottom of the try block:

      try {
        // original statements
        //...
        try {
          // statements in old finally block
          //...
        }
        catch { throw; }
      }
      catch {...}
        // original catch statements
      }

    Hans Passant.
    Sunday, July 26, 2009 9:34 AM