Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to know when finally block actually executed.finally block executed after the exception occurred or will executed normally?
code snippet:
C#
try
{
   if (txt_password.Text == txt_cpassword.Text)
   {
     con.Open();
     cmd = new SqlCommand("DELETE FROM LoginTB WHERE Username = '" + txt_username.Text + "' AND Password = '" + txt_cpassword.Text + "'", con);
     cmd.ExecuteNonQuery();
     MessageBox.Show("User account Deleted");
   }
    else
      {
       MessageBox.Show("Sorry...Password does not match with confirm password ");
      }
}
catch (Exception)
  {
   MessageBox.Show("Invalid operation");
  }
finally
  {
   con.Close();
  }

If try - catch block don't give me any exception means it normally executed to its desired result.
if I write con.Close() in finally block is there connection close or I have to write con.Close() in try block.

Thanking you.
Posted
Updated 6-Nov-16 18:20pm
v2
Comments
PIEBALDconsult 23-Nov-13 23:18pm    
I agree with Rohan, and would add that the Open should be outside the try/catch. Better would be to move the data access code into a separate class (Data Access Layer).

In very simple terms,finally is guaranteed to execute regardless of exception. See this : try-catch-finally (C# Reference)[^]

It states that:
Quote:
A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

Side note:

It is always good to use parameterized query. That is,
C#
DELETE FROM LoginTB WHERE Username = '" + txt_username.Text + "' AND Password = '" + txt_cpassword.Text + "'
should be modified to prevent SQL injection attacks.

See this : Using parameterized queries to prevent sql injection attacks in sql server[^]
 
Share this answer
 
v3
Comments
RaisKazi 23-Nov-13 22:51pm    
My 5! Complete Answer.
Thanks7872 23-Nov-13 23:11pm    
Thank you :-)
Sergey Alexandrovich Kryukov 24-Nov-13 12:45pm    
Sure, a 5.
—SA
Thanks7872 24-Nov-13 20:08pm    
Thanks Sergey.
The finally block always executes - irrespective of whether there is an error or not.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Nov-13 12:46pm    
What's good about it — just to the point, a 5.
—SA
Abhinav S 24-Nov-13 12:49pm    
Thank you.
The rule of thumb is "open as late as possible. Close asap". Use "using" statement will ensure that.

Take a look at http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx.

For this reason, I usually do this:

using(MySqlConnection con= new MySqlConnetion(connectionString)
{
con.open();
// perform database query here

} //con gets closed and disposed immediately upon exits


There is a good example here:
http://www.dotnetperls.com/sqlconnection
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Nov-13 12:48pm    
This principle is at least questionable. I would say, it depends on open/close what...
—SA
Execution of finally block is regardless of whether exception is occurs or not. It always gets executed followed by try block(if exception not occurred) or followed by try-catch block(if exception occurs). Finally block always used to put cleanup code such as Closing database connections, Closing of file handlers, etc.
 
Share this answer
 
Comments
[no name] 7-Nov-16 7:02am    
Pretty sure the OP got the idea from all of the answers from THREE years ago.
CHill60 12-Nov-16 21:23pm    
My advice is to (in general) avoid answering questions that are more than, say, 6 months old. Especially avoid answering questions that already have an answer posted, especially if one or more of the answers have been "Accepted" or have 4 stars or more average rating.
But most importantly, really really avoid just saying the same thing that someone else has already said.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900