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 / C++ / MFC
  4. return in catch block

return in catch block

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingvisual-studiographicshelp
9 Posts 5 Posters 1 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.
  • C Offline
    C Offline
    clawton
    wrote on last edited by
    #1

    Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block? For example: try { // Some code... } catch (std::exception &) { // log error return -1; } Also try this test using VC with VS 2005 SP1 (Microsoft Visual C++ 2005 77626-009-0000007-41138) 1) Create standard MFC dialog based application 2) Add new method with signature: int foo(); 3) in foo add the following code: std::vector v; try { v.push_back(1); } catch (std::exception &;) { return 1; } // Just some code that does something... if (v.size()) { } return 0; 4) Step through the code in debugger. On several systems, I see the debug statement indicator move onto the line with "return 1". The disassembly code is very strange indeed. Any comments from the guru's out there? :) Thanks!

    S L E E 4 Replies Last reply
    0
    • C clawton

      Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block? For example: try { // Some code... } catch (std::exception &) { // log error return -1; } Also try this test using VC with VS 2005 SP1 (Microsoft Visual C++ 2005 77626-009-0000007-41138) 1) Create standard MFC dialog based application 2) Add new method with signature: int foo(); 3) in foo add the following code: std::vector v; try { v.push_back(1); } catch (std::exception &;) { return 1; } // Just some code that does something... if (v.size()) { } return 0; 4) Step through the code in debugger. On several systems, I see the debug statement indicator move onto the line with "return 1". The disassembly code is very strange indeed. Any comments from the guru's out there? :) Thanks!

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      clawton wrote:

      Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block

      Should be fine.

      clawton wrote:

      Step through the code in debugger.   On several systems, I see the debug statement indicator move onto the line with "return 1".

      Yeah, that happens - it all depends how the debug information's been distributed through the code. Is it in Release or Debug mode? Debugging in Release mode is...interesting.

      clawton wrote:

      The disassembly code is very strange indeed

      In what sense?

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      C 1 Reply Last reply
      0
      • S Stuart Dootson

        clawton wrote:

        Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block

        Should be fine.

        clawton wrote:

        Step through the code in debugger.   On several systems, I see the debug statement indicator move onto the line with "return 1".

        Yeah, that happens - it all depends how the debug information's been distributed through the code. Is it in Release or Debug mode? Debugging in Release mode is...interesting.

        clawton wrote:

        The disassembly code is very strange indeed

        In what sense?

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        C Offline
        C Offline
        clawton
        wrote on last edited by
        #3

        Stuart Dootson wrote: Is it in Release or Debug mode? Debug mode. Stuart Dootson wrote: clawton wrote: The disassembly code is very strange indeed In what sense? Well, I see the line with "return 1" in there twice...:confused: Pasting from the dissassembly output window: std::vector<int> v; 004131C6 lea ecx,[ebp-34h] 004131C9 call std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> > (4110C3h) 004131CE mov dword ptr [ebp-4],0 try { 004131D5 mov byte ptr [ebp-4],1 v.push_back(1); 004131D9 mov dword ptr [ebp-118h],1 004131E3 lea eax,[ebp-118h] 004131E9 push eax 004131EA lea ecx,[ebp-34h] 004131ED call std::vector<int,std::allocator<int> >::push_back (4119B5h) 004131F2 jmp $LN5 (413211h) } catch (std::exception &) { return 1; 004131F4 mov dword ptr [ebp-10Ch],1 004131FE mov dword ptr [ebp-4],0 00413205 mov eax,offset $LN9 (41321Ah) 0041320A ret void CCatchTestDlg::OnBnClickedOk() { Test(1,0); } int CCatchTestDlg::Test(int foo, unsigned char * goo) { std::vector<int> v; try { v.push_back(1); } catch (std::exception &) { return 1; } 0041320B mov eax,offset $LN5 (413211h) 00413210 ret } catch (std::exception &) { return 1; 00413211 mov dword ptr [ebp-4],0 00413218 jmp $LN9+17h (413231h) $LN9: 0041321A mov dword ptr [ebp-4],0FFFFFFFFh 00413221 lea ecx,[ebp-34h] 00413224 call std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> > (41192Eh) 00413229 mov eax,dword ptr [ebp-10Ch] 0041322F jmp $LN9+54h (41326Eh) if (v.size()) { 00413231 lea ecx,[ebp-34h] 00413234 call std::vector<int,std::allocator<int> >::size (4115A5h)

        S 2 Replies Last reply
        0
        • C clawton

          Stuart Dootson wrote: Is it in Release or Debug mode? Debug mode. Stuart Dootson wrote: clawton wrote: The disassembly code is very strange indeed In what sense? Well, I see the line with "return 1" in there twice...:confused: Pasting from the dissassembly output window: std::vector<int> v; 004131C6 lea ecx,[ebp-34h] 004131C9 call std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> > (4110C3h) 004131CE mov dword ptr [ebp-4],0 try { 004131D5 mov byte ptr [ebp-4],1 v.push_back(1); 004131D9 mov dword ptr [ebp-118h],1 004131E3 lea eax,[ebp-118h] 004131E9 push eax 004131EA lea ecx,[ebp-34h] 004131ED call std::vector<int,std::allocator<int> >::push_back (4119B5h) 004131F2 jmp $LN5 (413211h) } catch (std::exception &) { return 1; 004131F4 mov dword ptr [ebp-10Ch],1 004131FE mov dword ptr [ebp-4],0 00413205 mov eax,offset $LN9 (41321Ah) 0041320A ret void CCatchTestDlg::OnBnClickedOk() { Test(1,0); } int CCatchTestDlg::Test(int foo, unsigned char * goo) { std::vector<int> v; try { v.push_back(1); } catch (std::exception &) { return 1; } 0041320B mov eax,offset $LN5 (413211h) 00413210 ret } catch (std::exception &) { return 1; 00413211 mov dword ptr [ebp-4],0 00413218 jmp $LN9+17h (413231h) $LN9: 0041321A mov dword ptr [ebp-4],0FFFFFFFFh 00413221 lea ecx,[ebp-34h] 00413224 call std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> > (41192Eh) 00413229 mov eax,dword ptr [ebp-10Ch] 0041322F jmp $LN9+54h (41326Eh) if (v.size()) { 00413231 lea ecx,[ebp-34h] 00413234 call std::vector<int,std::allocator<int> >::size (4115A5h)

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          I think I see 'return 1;' three times... I think the reason is that there's more code thn you expect round an exception handler (unhandled case, handled case, no exception case) and the disassembler puts in hte source code context for each one.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • C clawton

            Stuart Dootson wrote: Is it in Release or Debug mode? Debug mode. Stuart Dootson wrote: clawton wrote: The disassembly code is very strange indeed In what sense? Well, I see the line with "return 1" in there twice...:confused: Pasting from the dissassembly output window: std::vector<int> v; 004131C6 lea ecx,[ebp-34h] 004131C9 call std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> > (4110C3h) 004131CE mov dword ptr [ebp-4],0 try { 004131D5 mov byte ptr [ebp-4],1 v.push_back(1); 004131D9 mov dword ptr [ebp-118h],1 004131E3 lea eax,[ebp-118h] 004131E9 push eax 004131EA lea ecx,[ebp-34h] 004131ED call std::vector<int,std::allocator<int> >::push_back (4119B5h) 004131F2 jmp $LN5 (413211h) } catch (std::exception &) { return 1; 004131F4 mov dword ptr [ebp-10Ch],1 004131FE mov dword ptr [ebp-4],0 00413205 mov eax,offset $LN9 (41321Ah) 0041320A ret void CCatchTestDlg::OnBnClickedOk() { Test(1,0); } int CCatchTestDlg::Test(int foo, unsigned char * goo) { std::vector<int> v; try { v.push_back(1); } catch (std::exception &) { return 1; } 0041320B mov eax,offset $LN5 (413211h) 00413210 ret } catch (std::exception &) { return 1; 00413211 mov dword ptr [ebp-4],0 00413218 jmp $LN9+17h (413231h) $LN9: 0041321A mov dword ptr [ebp-4],0FFFFFFFFh 00413221 lea ecx,[ebp-34h] 00413224 call std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> > (41192Eh) 00413229 mov eax,dword ptr [ebp-10Ch] 0041322F jmp $LN9+54h (41326Eh) if (v.size()) { 00413231 lea ecx,[ebp-34h] 00413234 call std::vector<int,std::allocator<int> >::size (4115A5h)

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            I think I see 'return 1;' three times... I think the reason is that there's more code thn you expect round an exception handler (unhandled case, handled case, no exception case) and the disassembler puts in the source code context for each one.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            1 Reply Last reply
            0
            • C clawton

              Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block? For example: try { // Some code... } catch (std::exception &) { // log error return -1; } Also try this test using VC with VS 2005 SP1 (Microsoft Visual C++ 2005 77626-009-0000007-41138) 1) Create standard MFC dialog based application 2) Add new method with signature: int foo(); 3) in foo add the following code: std::vector v; try { v.push_back(1); } catch (std::exception &;) { return 1; } // Just some code that does something... if (v.size()) { } return 0; 4) Step through the code in debugger. On several systems, I see the debug statement indicator move onto the line with "return 1". The disassembly code is very strange indeed. Any comments from the guru's out there? :) Thanks!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, some people insist on having just one return statement in a function; that makes having it in a catch block rather hard. If you feel more lenient, go ahead. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              1 Reply Last reply
              0
              • C clawton

                Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block? For example: try { // Some code... } catch (std::exception &) { // log error return -1; } Also try this test using VC with VS 2005 SP1 (Microsoft Visual C++ 2005 77626-009-0000007-41138) 1) Create standard MFC dialog based application 2) Add new method with signature: int foo(); 3) in foo add the following code: std::vector v; try { v.push_back(1); } catch (std::exception &;) { return 1; } // Just some code that does something... if (v.size()) { } return 0; 4) Step through the code in debugger. On several systems, I see the debug statement indicator move onto the line with "return 1". The disassembly code is very strange indeed. Any comments from the guru's out there? :) Thanks!

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #7

                I guess there's nothing so wrong with returning -1 from catch block. But I personally, just HATE it. :)


                OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                1 Reply Last reply
                0
                • C clawton

                  Are there any rules of thumb, guidelines, issues with, etc... about having a return in a catch block? For example: try { // Some code... } catch (std::exception &) { // log error return -1; } Also try this test using VC with VS 2005 SP1 (Microsoft Visual C++ 2005 77626-009-0000007-41138) 1) Create standard MFC dialog based application 2) Add new method with signature: int foo(); 3) in foo add the following code: std::vector v; try { v.push_back(1); } catch (std::exception &;) { return 1; } // Just some code that does something... if (v.size()) { } return 0; 4) Step through the code in debugger. On several systems, I see the debug statement indicator move onto the line with "return 1". The disassembly code is very strange indeed. Any comments from the guru's out there? :) Thanks!

                  E Offline
                  E Offline
                  ehaerim
                  wrote on last edited by
                  #8

                  Hi Clawton I have the same issue. It seems the same happens in VC2010 too. First I tried to find out what's wrong in my code, but a few days trial failed and I came here after searching internet. Now I see it's not my code problem but the compiler. Have you found out how to make the debugger not step into the 'return 1'? All the replies below do not clearly commented why it happens and how to overcome it. If you summarize your answers that'd be great. thx HaeRim

                  C 1 Reply Last reply
                  0
                  • E ehaerim

                    Hi Clawton I have the same issue. It seems the same happens in VC2010 too. First I tried to find out what's wrong in my code, but a few days trial failed and I came here after searching internet. Now I see it's not my code problem but the compiler. Have you found out how to make the debugger not step into the 'return 1'? All the replies below do not clearly commented why it happens and how to overcome it. If you summarize your answers that'd be great. thx HaeRim

                    C Offline
                    C Offline
                    clawton
                    wrote on last edited by
                    #9

                    After seeing the replies and studying the assemble code, I ended up convencing myself that all was operating as it should and I need to just be aware of it. Although I do try to code a bit differently - i.e. not put a return in a catch block. Chris

                    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