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 Update on Monday, May 27th 2025

try-finally statement behaviour in MSVC C++

Scheduled Pinned Locked Moved Solved C++ Gurus
12 Posts 3 Posters 2.5k Views
  • 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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 ?

    aha_1980A 1 Reply Last reply
    0
    • JonBJ JonB

      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 ?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt has to stay free or it will die.

      JonBJ 1 Reply Last reply
      3
      • aha_1980A aha_1980

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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?

        kshegunovK 1 Reply Last reply
        0
        • JonBJ JonB

          @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?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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

          JonBJ 1 Reply Last reply
          1
          • kshegunovK kshegunov

            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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.)

            kshegunovK 1 Reply Last reply
            0
            • JonBJ JonB

              @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.)

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

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

              Read and abide by the Qt Code of Conduct

              JonBJ 1 Reply Last reply
              0
              • kshegunovK kshegunov

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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...)?

                kshegunovK 1 Reply Last reply
                0
                • JonBJ JonB

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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

                  JonBJ 1 Reply Last reply
                  2
                  • kshegunovK kshegunov

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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?

                    kshegunovK 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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?

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 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

                      JonBJ 1 Reply Last reply
                      1
                      • kshegunovK kshegunov

                        @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.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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!!!

                        kshegunovK 1 Reply Last reply
                        0
                        • JonBJ JonB

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

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 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

                          • Login

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