Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: When the finally code is executed? Pin
musefan1-Jul-09 0:55
musefan1-Jul-09 0:55 
GeneralRe: When the finally code is executed? Pin
musefan1-Jul-09 2:08
musefan1-Jul-09 2:08 
GeneralRe: When the finally code is executed? Pin
J4amieC1-Jul-09 2:12
J4amieC1-Jul-09 2:12 
GeneralRe: When the finally code is executed? Pin
musefan1-Jul-09 2:19
musefan1-Jul-09 2:19 
GeneralRe: When the finally code is executed? Pin
dojohansen1-Jul-09 5:44
dojohansen1-Jul-09 5:44 
GeneralRe: When the finally code is executed? Pin
dojohansen1-Jul-09 5:37
dojohansen1-Jul-09 5:37 
GeneralRe: When the finally code is executed? Pin
dojohansen1-Jul-09 5:59
dojohansen1-Jul-09 5:59 
AnswerRe: When the finally code is executed? Pin
dojohansen30-Jun-09 22:25
dojohansen30-Jun-09 22:25 
Hi,

Your code wouldn't actually compile because you've scoped i to the try block but use it also in the finally block.

That said, any code of a method that executes at all *necessarily* does so *before* the method returns. BUT... your method, if you move the int i = 1; line before the try-block (so it can compile), will nonetheless return 1.

Logically you can think of it as transforming the code into something like this:

int i = 1;
try
{
  int returnValue = i;
  i += 3; 
  return returnValue;
}
catch
{
  i += 3;
  throw;
}


In other words, the finally code executes prior to returning, but everything else, including evaluating the return value, happens before. You can see this by taking your try-finally code, set a breakpoint at entry, and step through it using "step into" (F11 in the English Visual Studio).

However, while it is *possible* to write code that is a bit confusing using try-finally or try-catch-finally, in practice there is no real problem. Catch blocks are for error handling and finally blocks are for freeing up resources. The classic example is databases and transactions: Let's say your method needs to open a connection and start a transaction to insert a record into two tables. If either insert fails you want both to fail, ensuring the consistency of your database, and regardless of how the inserts go you must always close the connection before returning from the method. In other words, error handling consists of rolling back a failed transaction, and resource cleanup consists of closing (and thus freeing up) the connection. The code thus would be something like this:

SqlConnection cnx = new SqlConnection("Data Source=.; Initial Catalog=Northwind; Integrated Security=true");
cnx.Open();
SqlTransaction tx = null;
try
{
   tx = cnx.BeginTransaction();
   insertFirst();
   insertSecond();
   tx.Commit();
}
catch
{
   if (tx != null) tx.Rollback();
   throw;
}
finally
{
   cnx.Close();
}


If any statement in the try-block fails, control passes to the catch block. If BeginTransaction() failed tx is null and no rollback is attempted; if anything else fails we have a transaction and it is rolled back. Whether or not the operation fails, the connection is closed before the method either returns normally or re-throws the exception that occured during the try-block.

Hope this helps!
AnswerRe: When the finally code is executed? Pin
Fired.Fish.Gmail1-Jul-09 0:39
Fired.Fish.Gmail1-Jul-09 0:39 
GeneralRe: When the finally code is executed? Pin
dojohansen1-Jul-09 0:43
dojohansen1-Jul-09 0:43 
GeneralRe: When the finally code is executed? Pin
Fired.Fish.Gmail1-Jul-09 0:46
Fired.Fish.Gmail1-Jul-09 0:46 
QuestionTrouble with making multiple httpWebRequest "Get"'s to a webservice in a short time Pin
nineismine30-Jun-09 20:39
nineismine30-Jun-09 20:39 
AnswerRe: Trouble with making multiple httpWebRequest "Get"'s to a webservice in a short time Pin
Philip.F30-Jun-09 20:49
Philip.F30-Jun-09 20:49 
GeneralRe: Trouble with making multiple httpWebRequest "Get"'s to a webservice in a short time Pin
kstls30-Jun-09 22:50
kstls30-Jun-09 22:50 
GeneralRe: Trouble with making multiple httpWebRequest "Get"'s to a webservice in a short time Pin
nineismine1-Jul-09 2:29
nineismine1-Jul-09 2:29 
GeneralRe: Trouble with making multiple httpWebRequest "Get"'s to a webservice in a short time Pin
Philip.F1-Jul-09 3:18
Philip.F1-Jul-09 3:18 
QuestionData Entry in GridView Control Pin
Mads11530-Jun-09 20:08
Mads11530-Jun-09 20:08 
AnswerRe: Data Entry in GridView Control Pin
Chetan Patel30-Jun-09 20:15
Chetan Patel30-Jun-09 20:15 
AnswerRe: Data Entry in GridView Control Pin
Niladri_Biswas30-Jun-09 20:23
Niladri_Biswas30-Jun-09 20:23 
QuestionError occure while creating Object of COM dll Pin
Chetan Patel30-Jun-09 19:55
Chetan Patel30-Jun-09 19:55 
QuestionBest place to start? Pin
richforall30-Jun-09 18:57
richforall30-Jun-09 18:57 
AnswerRe: Best place to start? Pin
Niladri_Biswas30-Jun-09 20:34
Niladri_Biswas30-Jun-09 20:34 
AnswerRe: Best place to start? Pin
Blikkies30-Jun-09 22:41
professionalBlikkies30-Jun-09 22:41 
GeneralRe: Best place to start? Pin
PIEBALDconsult1-Jul-09 5:21
mvePIEBALDconsult1-Jul-09 5:21 
Questiondisplay a form inside a web browser Pin
tanweer30-Jun-09 17:28
tanweer30-Jun-09 17:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.