Search:
Welcome Guest | Register | Login
logo

Is finally block needed to close the connection, if yes then why?

One day my fellow worker asked my why we need finally block in try catch? we can achieve the same goal without using finally block by placing all the codes after catch block, like this

Try {
    open connection;
    }
    catch(Exception ex)
    {/* code to handle exception */ }
    close connection;

AND

Try {
    open connection;
    }
    catch(Exception ex)
    {/* code to handle exception */ }
    finally{  close connection; }

In 1st block I am trying to close connection outside the try catch

In 2nd block I am trying to close the connection in finally block, so what is the need of finally block?

In both case, error will be catch in catch block and connection will be closed, so what is the need of finally block.

  • ASP.Net
  • C#
1
 
Asked: 04 Mar 2013
Reputation: 44
John Clark
2 Answers
try{
    SqlConnection conn = new SqlConnection();
    try{
        // It will generate error: Format of the initialization string....
        Console.WriteLine("Initialize connection string.");
        conn.ConnectionString = "xyx";
    }
    catch (Exception ex){
        Console.WriteLine("Inner catch is called");
        throw new Exception(ex.Message);
    }
    finally{
        Console.WriteLine("Finally is called");
        conn.Dispose();
    }
}
catch (Exception outerex){
    Console.WriteLine("Outer catch is called");
}

And here is the output:

Initialize connection string.
Inner catch is called
Finally is called
Outer catch is called

So in above code our conn is disposed properly, now let's see without finally block

try{
    SqlConnection conn = new SqlConnection();
    try{
        // It will generate error: Format of the initialization string....
        Console.WriteLine("Initialize connection string.");
        conn.ConnectionString = "xyx";
    }
    catch (Exception ex){
        Console.WriteLine("Inner catch is called");
        throw new Exception(ex.Message);
    }
    Console.WriteLine("without finally try to dispose conn");
    conn.Dispose();
}
catch (Exception outerex){
    Console.WriteLine("Outer catch is called");
}    

And here is execution order:

Initialize connection string.
Inner catch is called
Outer catch is called

In this case "without finally try to dispose conn" conn.Dispose(); not called, so we can say, yes finally is needed.

2
 
Answered: 05 Mar 2013
Reputation: 1,109
Myghty

Finally block always executed in a try catch finally block even you throw the exception from catch block.

If you will try to catch and throw exception and write code after catch without finally block that will not execute and shown by Myghty.

There only some cases in which finally block will not execute

  • The thread running the try-catch-finally block is killed or interrupted
  • The underlying VS is destroyed in some other way
  • The underlying hardware is unusable in some way
1
 
Answered: 05 Mar 2013
Reputation: 252
Nathan Armour
Login to post your answer