locked
finally block... RRS feed

  • Question

  • Hello,

     

    I'm confused with finally block...Let me explain it:

    what is the difference with

    ---------------------------------------------------

    try

    {   /// throw an exception   }

    catch ( Exception ex)

    {   // do something   }

    //rest of the program

    -------------------------------------------------------

    and

    try

    {   /// throw an exception   }

    catch ( Exception ex)

    {   // do something   }

    finally{   //rest of the program   }

     

    ???

     

    nothing changes if finally exists or doesnt exist.... If exception occurs or not, flow goes into catch block and when leaves catch block it goes into finally.so why do we put finally if flow goes into finally if exception occurs or not...?

     

    Thanks very much...

    Monday, April 24, 2006 2:36 AM

Answers

  • From the helpfile:

    "The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.

    Whereas catch is used to handle exceptions that occur in a statement block, finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited"

    // try-finally
    using System;
    public class MainClass
    {
        static void Main()
        {
            int i = 123;
            string s = "Some string";
            object o = s;
    
            try
            {
                // Invalid conversion; o contains a string not an int
                i = (int)o;
            }
            finally
            {
                Console.Write("i = {0}", i);
            }
        }
    }

    Does that help?

    Monday, April 24, 2006 5:35 AM

All replies

  • From the helpfile:

    "The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.

    Whereas catch is used to handle exceptions that occur in a statement block, finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited"

    // try-finally
    using System;
    public class MainClass
    {
        static void Main()
        {
            int i = 123;
            string s = "Some string";
            object o = s;
    
            try
            {
                // Invalid conversion; o contains a string not an int
                i = (int)o;
            }
            finally
            {
                Console.Write("i = {0}", i);
            }
        }
    }

    Does that help?

    Monday, April 24, 2006 5:35 AM
  •  xyzt wrote:

    nothing changes if finally exists or doesnt exist....

    Yes it does. For example, if the exception is rethrown or you throw another exception from withint the catch block, the "rest of the program" part will not run if it's not in the finally block (as in your first code snippet).

     

    Monday, April 24, 2006 7:00 AM