Click here to Skip to main content
15,886,963 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: When the finally code is executed? Pin
Fired.Fish.Gmail1-Jul-09 0:39
Fired.Fish.Gmail1-Jul-09 0:39 
Thank you for everyone's replies.

I saw one question that if there is the return statement in the try block, and when the finally is to be executed.
I thought that when return, the finally statement will not be executed. otherwise ,if input one return statement in the finally or change the variable value in the finally block, the result will be changed.

The code in the subject can not be compiled, because I define the variable i and j of the int type in the try block.


Now I modify them.
Following is the new code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Finally
{
class Program
{
static void Main(string[] args)
{
int z = func1();
}

static public int func1()
{
int i = 1;
int j = 2;

try
{
i = 110;
j = 119;
return i;
}
finally
{
i += 3;
}
}
}
}

I find that the return statement can not be typed in the finally block.
From the asm code, we can find that the function save the return value and then jump to the finally block to execute. so the result will not be influenced.
Following is the asm code.

using System;
using System.Collections.Generic;
using System.Text;

namespace Finally
{
class Program
{
static void Main(string[] args)
{
int z = func1();
}

static public int func1()
{
00000000 push ebp
00000001 mov ebp,esp
00000003 push edi
00000004 push esi
00000005 push ebx
00000006 sub esp,38h
00000009 xor eax,eax
0000000b mov dword ptr [ebp-10h],eax
0000000e xor eax,eax
00000010 mov dword ptr [ebp-1Ch],eax
00000013 cmp dword ptr ds:[00AE8364h],0
0000001a je 00000021
0000001c call 792611C6
00000021 xor edx,edx
00000023 mov dword ptr [ebp-3Ch],edx
00000026 xor edx,edx
00000028 mov dword ptr [ebp-40h],edx
0000002b xor edx,edx
0000002d mov dword ptr [ebp-44h],edx
00000030 nop
int i = 1;
00000031 mov dword ptr [ebp-3Ch],1
int j = 2;
00000038 mov dword ptr [ebp-40h],2

try
{
0000003f nop
i = 110;
00000040 mov dword ptr [ebp-3Ch],6Eh
j = 119;
00000047 mov dword ptr [ebp-40h],77h
return i;
0000004e mov eax,dword ptr [ebp-3Ch]
00000051 mov dword ptr [ebp-44h],eax //save the return value
00000054 nop
00000055 mov dword ptr [ebp-20h],0
0000005c mov dword ptr [ebp-1Ch],0FCh
00000063 push 0E311B7h
00000068 jmp 0000006A
}
finally
{
0000006a nop
i += 3;
0000006b add dword ptr [ebp-3Ch],3
}
0000006f nop
00000070 pop eax
00000071 jmp eax
00000073 nop
}
00000074 mov eax,dword ptr [ebp-44h]
00000077 lea esp,[ebp-0Ch]
0000007a pop ebx
0000007b pop esi
0000007c pop edi
0000007d pop ebp
0000007e ret
0000007f mov dword ptr [ebp-1Ch],0
00000086 jmp 00000073
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 
AnswerRe: display a form inside a web browser Pin
Christian Graus30-Jun-09 18:33
protectorChristian Graus30-Jun-09 18:33 

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.