Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. When the finally code is executed?

When the finally code is executed?

Scheduled Pinned Locked Moved C#
question
24 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M musefan

    That's the same thing I said just using different words, so maybe before correcting me you should have spent time understanding what I said and not just repeating me and thinking that you have given a different answer.

    Life goes very fast. Tomorrow, today is already yesterday.

    D Offline
    D Offline
    dojohansen
    wrote on last edited by
    #9

    I'll try to remember you're an ungrateful little brat. Actually you didn't say half of what I did, although you did say something vaguely similar. (Don't reply just to repeat a claim - readers can just look at the thread and decide for themselves.)

    J M 2 Replies Last reply
    0
    • D dojohansen

      I'll try to remember you're an ungrateful little brat. Actually you didn't say half of what I did, although you did say something vaguely similar. (Don't reply just to repeat a claim - readers can just look at the thread and decide for themselves.)

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #10

      You know, going round insulting the regulars isn't a good way to endear yourself to a community.

      D 1 Reply Last reply
      0
      • F Fired Fish Gmail

        try { int i = 1; int j = 2; return i; } finally { i += 3; } The code in the finally statement will be executed ? when? before return or after return ?

        F Offline
        F Offline
        Fired Fish Gmail
        wrote on last edited by
        #11

        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

        D 1 Reply Last reply
        0
        • J J4amieC

          You know, going round insulting the regulars isn't a good way to endear yourself to a community.

          D Offline
          D Offline
          dojohansen
          wrote on last edited by
          #12

          And you think I deserved the snotty reply he gave me? I think I contributed a very clear explanation of what exactly happens and provided more information than he had done (his explanation was onto something, but didn't point out how reference versus value types would affect the outcome - so I had something to add). It's up to you whether you rate contributions according to how regularly the author posts messages. I'd rather rate them for quality and value. And I reserve the right to return rudeness to anyone, regular or not.

          1 Reply Last reply
          0
          • F Fired Fish Gmail

            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

            D Offline
            D Offline
            dojohansen
            wrote on last edited by
            #13

            Indeed; this is what I explained, but thanks for the disassembly to prove it. Just one tip: Wrap your code in <pre></pre> tags and it will make a world of difference for readability!

            F 1 Reply Last reply
            0
            • D dojohansen

              Indeed; this is what I explained, but thanks for the disassembly to prove it. Just one tip: Wrap your code in <pre></pre> tags and it will make a world of difference for readability!

              F Offline
              F Offline
              Fired Fish Gmail
              wrote on last edited by
              #14

              I know that, thks.

              1 Reply Last reply
              0
              • D dojohansen

                I'll try to remember you're an ungrateful little brat. Actually you didn't say half of what I did, although you did say something vaguely similar. (Don't reply just to repeat a claim - readers can just look at the thread and decide for themselves.)

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #15

                Ungrateful for what? you didn't post anything that was designed to help me. I said that the return is executed before the 'finally code' but the function is not complete (returned) until the finally code is processed.

                dojohansen wrote:

                Actually you didn't say half of what I did

                I was not trying to claim to have said what you did - I said you said what I said. Anyway, my point is I don't appreciate something thinking there correcting me by basically saying the same thing. If I said: "1 + 1 = 2" and then someone said: "No, Actually, one add one equals 2" then that person would be considered some sort of idiot, don't you think?

                Life goes very fast. Tomorrow, today is already yesterday.

                M 1 Reply Last reply
                0
                • M musefan

                  Ungrateful for what? you didn't post anything that was designed to help me. I said that the return is executed before the 'finally code' but the function is not complete (returned) until the finally code is processed.

                  dojohansen wrote:

                  Actually you didn't say half of what I did

                  I was not trying to claim to have said what you did - I said you said what I said. Anyway, my point is I don't appreciate something thinking there correcting me by basically saying the same thing. If I said: "1 + 1 = 2" and then someone said: "No, Actually, one add one equals 2" then that person would be considered some sort of idiot, don't you think?

                  Life goes very fast. Tomorrow, today is already yesterday.

                  M Offline
                  M Offline
                  musefan
                  wrote on last edited by
                  #16

                  Just for any who are interested then dojohansen sent me an email with the following content... Hi, I don't want the thread to degenerate into a petty fight between you and me about whether or not we contributed something. But I do want to replay to *you* - hence this private message. musefan wrote:Ungrateful for what? you didn't post anything that was designed to help me. I said that the return is executed before the 'finally code' but the function is not complete (returned) until the finally code is processed. I definitely said something that was meant to help, although it was not meant for you specifically. A reply in a forum is a reply to a message, not to the author of the message - if I wanted to address you I'd send a private message like this one. What you said (as you accurately repeat in the above quote) is not wrong, but nor is it very precise. There's no mention of the crucial fact that the return value is evaluated before the finally executes**[I said that the 'finally block' executes after the return - which to me is the same as saying "the return value is evaluated before the finally executes"]. There is also nothing that can help your reader understand that it would make a difference if the method worked with a reference type rather than a value type[personally I see the OP as simply asking if finally code is executed before or after the return statement. Not "how do I add to a variable before returning the value?"]. So although I feel people ought to be very happy to be corrected, and I try to be so myself when I am, this message was not even an attempt to correct you. I merely tried to clarify further and add some information regarding the subtleties that arise from the fact that the return value is evaluated before the finally block runs and returned afterwards. To this, you gave a reply that was less than useless. Having first misinterpreted my message as a correction you decided to give me a snotty reply accusing me of trying to score brownie points by repeating you[I did not accuse you of 'stealing' my answer]**. Frankly I feel that if you thought that was what I was doing you should be kind enough to the rest of the community to either just forget about it, or alternatively bring it up in a private message. [the impression I got (intended or not) was that you were correcting me and then saying it was the same answer - to me that warrants a telling off] musefan wrote:Anyway, my point is I don't appreciate something thinking there corre

                  J D 3 Replies Last reply
                  0
                  • M musefan

                    Just for any who are interested then dojohansen sent me an email with the following content... Hi, I don't want the thread to degenerate into a petty fight between you and me about whether or not we contributed something. But I do want to replay to *you* - hence this private message. musefan wrote:Ungrateful for what? you didn't post anything that was designed to help me. I said that the return is executed before the 'finally code' but the function is not complete (returned) until the finally code is processed. I definitely said something that was meant to help, although it was not meant for you specifically. A reply in a forum is a reply to a message, not to the author of the message - if I wanted to address you I'd send a private message like this one. What you said (as you accurately repeat in the above quote) is not wrong, but nor is it very precise. There's no mention of the crucial fact that the return value is evaluated before the finally executes**[I said that the 'finally block' executes after the return - which to me is the same as saying "the return value is evaluated before the finally executes"]. There is also nothing that can help your reader understand that it would make a difference if the method worked with a reference type rather than a value type[personally I see the OP as simply asking if finally code is executed before or after the return statement. Not "how do I add to a variable before returning the value?"]. So although I feel people ought to be very happy to be corrected, and I try to be so myself when I am, this message was not even an attempt to correct you. I merely tried to clarify further and add some information regarding the subtleties that arise from the fact that the return value is evaluated before the finally block runs and returned afterwards. To this, you gave a reply that was less than useless. Having first misinterpreted my message as a correction you decided to give me a snotty reply accusing me of trying to score brownie points by repeating you[I did not accuse you of 'stealing' my answer]**. Frankly I feel that if you thought that was what I was doing you should be kind enough to the rest of the community to either just forget about it, or alternatively bring it up in a private message. [the impression I got (intended or not) was that you were correcting me and then saying it was the same answer - to me that warrants a telling off] musefan wrote:Anyway, my point is I don't appreciate something thinking there corre

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #17

                    He's obviously a self riteous idiot who likes the sound of his own voice (or rather the look of his own typing). Ive not even seen the name here before today, but in the last few hours he's answering question after question in the most verbose way possible.. often replying to other satisfactory answers (like yours above) just to add his 2pence worth. Ignore him. You do a great job of answering questions here.

                    M D 2 Replies Last reply
                    0
                    • J J4amieC

                      He's obviously a self riteous idiot who likes the sound of his own voice (or rather the look of his own typing). Ive not even seen the name here before today, but in the last few hours he's answering question after question in the most verbose way possible.. often replying to other satisfactory answers (like yours above) just to add his 2pence worth. Ignore him. You do a great job of answering questions here.

                      M Offline
                      M Offline
                      musefan
                      wrote on last edited by
                      #18

                      :) Thanks for your appreciation - I will take your advise and ignore any more responses ;)

                      Life goes very fast. Tomorrow, today is already yesterday.

                      1 Reply Last reply
                      0
                      • D dojohansen

                        Well that is a dumb answer because it's totally unhelpful! You're right that it won't compile, but surely you can see that the question works by simply declaring i at the method level instead. And there is in fact a subtle effect of the try-finally.

                        int foo()
                        {
                        int i = 1;
                        try { return i; }
                        finally { i = 5; }
                        }

                        This code compiles, but what does foo() return? I think quite a lot of people would expect it to return 5, since the finally block is executed before the method returns. But the return value is *evaluated* before the finally block runs and the method returns 1. Now what would happen if we boxed the int in a class of our own?

                        class BoxedInt { public int Value; }

                        BoxedInt bar()
                        {
                        BoxedInt i = new BoxedInt() { Value = 1 };
                        try { return i; }
                        finally { i.Value = 5; }
                        }

                        While foo() returns 1, bar() returns a BoxedInt whose Value is 5. That's because BoxedInt is a class and thus a reference type, so it does not matter if the return value is evaluated before or after the finally block executes. Change BoxedInt to a struct, and bar will return a BoxedInt with Value equal to 1 as well. I'd say there's some subtlety here, so calling that a stupid question seems to me rather a stretch.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #19

                        So... you did try it, but asked the question anyway? :confused:

                        D 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          So... you did try it, but asked the question anyway? :confused:

                          D Offline
                          D Offline
                          dojohansen
                          wrote on last edited by
                          #20

                          Now that got me nicely confused. You replied to my message so presumably "you" means me. But what "it" and "the question" refers to is less clear. I tried many things including compiling and running the various code snippets I've put up on this thread. I may have asked questions too, but I am not the OP so if you were referring to the original message that might explain the confusion. If you did mean I tried something (and consequently found out what it does) and still asked what it does, let me know what it was and I will attempt to communicate whatever it was I was wondering about more clearly. :)

                          P 1 Reply Last reply
                          0
                          • M musefan

                            Just for any who are interested then dojohansen sent me an email with the following content... Hi, I don't want the thread to degenerate into a petty fight between you and me about whether or not we contributed something. But I do want to replay to *you* - hence this private message. musefan wrote:Ungrateful for what? you didn't post anything that was designed to help me. I said that the return is executed before the 'finally code' but the function is not complete (returned) until the finally code is processed. I definitely said something that was meant to help, although it was not meant for you specifically. A reply in a forum is a reply to a message, not to the author of the message - if I wanted to address you I'd send a private message like this one. What you said (as you accurately repeat in the above quote) is not wrong, but nor is it very precise. There's no mention of the crucial fact that the return value is evaluated before the finally executes**[I said that the 'finally block' executes after the return - which to me is the same as saying "the return value is evaluated before the finally executes"]. There is also nothing that can help your reader understand that it would make a difference if the method worked with a reference type rather than a value type[personally I see the OP as simply asking if finally code is executed before or after the return statement. Not "how do I add to a variable before returning the value?"]. So although I feel people ought to be very happy to be corrected, and I try to be so myself when I am, this message was not even an attempt to correct you. I merely tried to clarify further and add some information regarding the subtleties that arise from the fact that the return value is evaluated before the finally block runs and returned afterwards. To this, you gave a reply that was less than useless. Having first misinterpreted my message as a correction you decided to give me a snotty reply accusing me of trying to score brownie points by repeating you[I did not accuse you of 'stealing' my answer]**. Frankly I feel that if you thought that was what I was doing you should be kind enough to the rest of the community to either just forget about it, or alternatively bring it up in a private message. [the impression I got (intended or not) was that you were correcting me and then saying it was the same answer - to me that warrants a telling off] musefan wrote:Anyway, my point is I don't appreciate something thinking there corre

                            D Offline
                            D Offline
                            dojohansen
                            wrote on last edited by
                            #21

                            I tried to take this off the thread because in my opinion personal differences do not belong to a thread where someone asks about a try-finally block, nor do I think it is the place to discuss forum etiquette if that is what you attempt to make it look like you're doing. Since you insist on polluting with this, I shall offer you one more insult for your fragile ego to consume today: You really are the biggest loser I've yet come across.

                            1 Reply Last reply
                            0
                            • J J4amieC

                              He's obviously a self riteous idiot who likes the sound of his own voice (or rather the look of his own typing). Ive not even seen the name here before today, but in the last few hours he's answering question after question in the most verbose way possible.. often replying to other satisfactory answers (like yours above) just to add his 2pence worth. Ignore him. You do a great job of answering questions here.

                              D Offline
                              D Offline
                              dojohansen
                              wrote on last edited by
                              #22

                              At least I'm a "self-rietous idiot" who can spell reasonably well. I've only posted a little over 200 messages since I became a member of CodeProject in 2006, partly because I often have too much work to do to fool around here, partly because I usually take some time to craft responses the way I think people ought to respond. I don't dictate how much detail should be provided in messages or how much care should go into things like clear explanations, correct English grammar and spelling, or code examples that provide clear illustration. But just like you guys presumably do, I post the sort of messages I think the forums need. And you know what? I've had quite a lot of people who appreciated it. Every one of those is worth about a hundred of those smallminded responses I sometimes get that seem motivated by a need to point out that I am no better than them. If I never said I was better and the only reason people react this way is because I provide a decent answer to a question, that's fine by me. It will be interesting to see if you take your own advice and just ignore me.

                              1 Reply Last reply
                              0
                              • M musefan

                                Just for any who are interested then dojohansen sent me an email with the following content... Hi, I don't want the thread to degenerate into a petty fight between you and me about whether or not we contributed something. But I do want to replay to *you* - hence this private message. musefan wrote:Ungrateful for what? you didn't post anything that was designed to help me. I said that the return is executed before the 'finally code' but the function is not complete (returned) until the finally code is processed. I definitely said something that was meant to help, although it was not meant for you specifically. A reply in a forum is a reply to a message, not to the author of the message - if I wanted to address you I'd send a private message like this one. What you said (as you accurately repeat in the above quote) is not wrong, but nor is it very precise. There's no mention of the crucial fact that the return value is evaluated before the finally executes**[I said that the 'finally block' executes after the return - which to me is the same as saying "the return value is evaluated before the finally executes"]. There is also nothing that can help your reader understand that it would make a difference if the method worked with a reference type rather than a value type[personally I see the OP as simply asking if finally code is executed before or after the return statement. Not "how do I add to a variable before returning the value?"]. So although I feel people ought to be very happy to be corrected, and I try to be so myself when I am, this message was not even an attempt to correct you. I merely tried to clarify further and add some information regarding the subtleties that arise from the fact that the return value is evaluated before the finally block runs and returned afterwards. To this, you gave a reply that was less than useless. Having first misinterpreted my message as a correction you decided to give me a snotty reply accusing me of trying to score brownie points by repeating you[I did not accuse you of 'stealing' my answer]**. Frankly I feel that if you thought that was what I was doing you should be kind enough to the rest of the community to either just forget about it, or alternatively bring it up in a private message. [the impression I got (intended or not) was that you were correcting me and then saying it was the same answer - to me that warrants a telling off] musefan wrote:Anyway, my point is I don't appreciate something thinking there corre

                                D Offline
                                D Offline
                                dojohansen
                                wrote on last edited by
                                #23

                                Since you're being such a pain in the back about this, I'll just keep it up.

                                musefan wrote:

                                I said that the 'finally block' executes after the return - which to me is the same as saying "the return value is evaluated before the finally executes"

                                Well at this point I cannot just add to what you're saying, because the finally block of course does NOT execute "after the return" - that would be impossible. No part of a method executes after it has returned, and that certainly includes the finally. And saying that is the same "to you" doesn't make it right. If you say "that's what I *meant* to say" I'll just take your word for it, mainly because I don't really think it matters if it is true or not - my goal in contributing in forums is to put some stuff in here for the community to learn about, not to craft replies for you personally. (However, when you take it totally off the thread topic, I prefer to switch to personal messages unless my opponent is so rude as to force me to publicly defend myself.)

                                musefan wrote:

                                I did not accuse you of 'stealing' my answer

                                Read your own reply that started this whole thing. Unless it was precisely that accusation, what content is there in the message? Are you trying to claim you just wanted me to know that you had already said the same thing I said? If so, why did you want me to have this piece of information?

                                musefan wrote:

                                yeah maybe I could have handled it differently but you clearly misunderstood my answer and therefore instead of 'correcting' it then you should say you don't understand

                                First, I didn't correct you, I merely gave what I see as a better explanation. The closest I got to hinting you might have said something less than accurate was to say it was "a bit more subtle". To my mind, this was a way of acknowledging that my additional comment concerned minute details, but it seems to have upset you instead. Second, who are you to tell me what I should and should not post? I am in fact a big fan of saying "I don't understand", but not when I do.

                                1 Reply Last reply
                                0
                                • D dojohansen

                                  Now that got me nicely confused. You replied to my message so presumably "you" means me. But what "it" and "the question" refers to is less clear. I tried many things including compiling and running the various code snippets I've put up on this thread. I may have asked questions too, but I am not the OP so if you were referring to the original message that might explain the confusion. If you did mean I tried something (and consequently found out what it does) and still asked what it does, let me know what it was and I will attempt to communicate whatever it was I was wondering about more clearly. :)

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #24

                                  Whoops, I meant to reply to the OP.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups