Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. try-finally statement behaviour in MSVC C++
Forum Updated to NodeBB v4.3 + New Features

try-finally statement behaviour in MSVC C++

Scheduled Pinned Locked Moved Solved C++ Gurus
12 Posts 3 Posters 2.5k Views 2 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.
  • J Offline
    J Offline
    JonB
    wrote on 28 Jun 2018, 09:41 last edited by JonB
    #1

    A quick special-interest query from me. Nothing to do with Qt, but addressed to anyone using Microsoft C++ compiler (I don't, so I can't test), just for my interest.

    MSVC has a non-standard extension to C++ of (https://msdn.microsoft.com/en-us/library/9xtt5hxz.aspx):

    __try {  
       // guarded code  
    }  
    __finally {  
       // termination code  
    } 
    

    I was wondering (idly) how it would handle the following:

    int func()
    {
        __try {  
            return 1; 
        }  
        __finally {  
            return 2;  
       }
    }
    

    What is the return value? Or does the compiler not allow a return statement inside its __finally ?

    A 1 Reply Last reply 28 Jun 2018, 09:50
    0
    • J JonB
      28 Jun 2018, 09:41

      A quick special-interest query from me. Nothing to do with Qt, but addressed to anyone using Microsoft C++ compiler (I don't, so I can't test), just for my interest.

      MSVC has a non-standard extension to C++ of (https://msdn.microsoft.com/en-us/library/9xtt5hxz.aspx):

      __try {  
         // guarded code  
      }  
      __finally {  
         // termination code  
      } 
      

      I was wondering (idly) how it would handle the following:

      int func()
      {
          __try {  
              return 1; 
          }  
          __finally {  
              return 2;  
         }
      }
      

      What is the return value? Or does the compiler not allow a return statement inside its __finally ?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 28 Jun 2018, 09:50 last edited by
      #2

      @JonB try yourself: https://godbolt.org/

      Qt has to stay free or it will die.

      J 1 Reply Last reply 28 Jun 2018, 11:11
      3
      • A aha_1980
        28 Jun 2018, 09:50

        @JonB try yourself: https://godbolt.org/

        J Offline
        J Offline
        JonB
        wrote on 28 Jun 2018, 11:11 last edited by
        #3

        @aha_1980
        Wow, thanks! I didn't know something like that existed, and didn't expect it to offer MSVC. I thought that was commercial (we use it as a paid product in our company), but I guess there's a free one too.

        It certainly shows me it compiles OK. However, looking through all the disassembler mov eax, <num> and ret 0 statements I'm unsure what it would actually return if run --- do you know?

        K 1 Reply Last reply 28 Jun 2018, 11:14
        0
        • J JonB
          28 Jun 2018, 11:11

          @aha_1980
          Wow, thanks! I didn't know something like that existed, and didn't expect it to offer MSVC. I thought that was commercial (we use it as a paid product in our company), but I guess there's a free one too.

          It certainly shows me it compiles OK. However, looking through all the disassembler mov eax, <num> and ret 0 statements I'm unsure what it would actually return if run --- do you know?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 28 Jun 2018, 11:14 last edited by kshegunov
          #4

          This comes from the Java world I believe. The idea is to have a handler for the both cases - an exception has occured and it was handled in the catch() (optional) and for when one didn't occur. Both (actually all three) paths would pass through the finally block before continuing on.

          Read and abide by the Qt Code of Conduct

          J 1 Reply Last reply 28 Jun 2018, 11:18
          1
          • K kshegunov
            28 Jun 2018, 11:14

            This comes from the Java world I believe. The idea is to have a handler for the both cases - an exception has occured and it was handled in the catch() (optional) and for when one didn't occur. Both (actually all three) paths would pass through the finally block before continuing on.

            J Offline
            J Offline
            JonB
            wrote on 28 Jun 2018, 11:18 last edited by JonB
            #5

            @kshegunov
            Yes, but the question is: I am deliberately nesting a return statement inside a try...finally where the finally has its own return statement. In this case, which value will the function return, 1 or 2? (I imagine it's the 2 in the __finally, but I wanted to confirm, given that I cannot run this.)

            K 1 Reply Last reply 28 Jun 2018, 11:19
            0
            • J JonB
              28 Jun 2018, 11:18

              @kshegunov
              Yes, but the question is: I am deliberately nesting a return statement inside a try...finally where the finally has its own return statement. In this case, which value will the function return, 1 or 2? (I imagine it's the 2 in the __finally, but I wanted to confirm, given that I cannot run this.)

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 28 Jun 2018, 11:19 last edited by
              #6

              1 would be my best guess, but I'm no expert.

              Read and abide by the Qt Code of Conduct

              J 1 Reply Last reply 28 Jun 2018, 11:20
              0
              • K kshegunov
                28 Jun 2018, 11:19

                1 would be my best guess, but I'm no expert.

                J Offline
                J Offline
                JonB
                wrote on 28 Jun 2018, 11:20 last edited by
                #7

                @kshegunov
                Hmm, but 2 would be my guess, and we can't both be right here (unless it's a quantum computer...)?

                K 1 Reply Last reply 28 Jun 2018, 11:25
                0
                • J JonB
                  28 Jun 2018, 11:20

                  @kshegunov
                  Hmm, but 2 would be my guess, and we can't both be right here (unless it's a quantum computer...)?

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 28 Jun 2018, 11:25 last edited by
                  #8

                  Glancing at the asm, courtesy of @aha_1980, I'm changing my stance to 2. At least for MSVC 2015 x86_64.

                  Read and abide by the Qt Code of Conduct

                  J 1 Reply Last reply 28 Jun 2018, 11:29
                  2
                  • K kshegunov
                    28 Jun 2018, 11:25

                    Glancing at the asm, courtesy of @aha_1980, I'm changing my stance to 2. At least for MSVC 2015 x86_64.

                    J Offline
                    J Offline
                    JonB
                    wrote on 28 Jun 2018, 11:29 last edited by
                    #9

                    @kshegunov
                    That sounds more reasonable :)
                    BTW, what are those ret 0 statements then? Does x86/x64 only use eax for return result, because ret 0 looks like some kind of "return 0" to me?

                    K 1 Reply Last reply 28 Jun 2018, 11:45
                    0
                    • J JonB
                      28 Jun 2018, 11:29

                      @kshegunov
                      That sounds more reasonable :)
                      BTW, what are those ret 0 statements then? Does x86/x64 only use eax for return result, because ret 0 looks like some kind of "return 0" to me?

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 28 Jun 2018, 11:45 last edited by
                      #10

                      @JonB said in try-finally statement behaviour in MSVC C++:

                      Does x86/x64 only use eax for return result

                      Yes.

                      because ret 0 looks like some kind of "return 0" to me?

                      Means return to the near address and pop so many bytes (i.e. 0) from the stack.

                      Read and abide by the Qt Code of Conduct

                      J 1 Reply Last reply 28 Jun 2018, 11:47
                      1
                      • K kshegunov
                        28 Jun 2018, 11:45

                        @JonB said in try-finally statement behaviour in MSVC C++:

                        Does x86/x64 only use eax for return result

                        Yes.

                        because ret 0 looks like some kind of "return 0" to me?

                        Means return to the near address and pop so many bytes (i.e. 0) from the stack.

                        J Offline
                        J Offline
                        JonB
                        wrote on 28 Jun 2018, 11:47 last edited by
                        #11

                        @kshegunov
                        Ohhhh, the 0 is "remove bytes from stack", not "pass 0 back to the caller". I would not have guessed that!!!

                        K 1 Reply Last reply 28 Jun 2018, 12:19
                        0
                        • J JonB
                          28 Jun 2018, 11:47

                          @kshegunov
                          Ohhhh, the 0 is "remove bytes from stack", not "pass 0 back to the caller". I would not have guessed that!!!

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 28 Jun 2018, 12:19 last edited by
                          #12

                          @JonB said in try-finally statement behaviour in MSVC C++:

                          I would not have guessed that!!!

                          Yep, it's rather rarely used by the compilers at least from my observations. The usual thing you'd see is just: ret, which is the same as ret 0.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          2

                          1/12

                          28 Jun 2018, 09:41

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved