return in catch block
-
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!
-
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!
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
-
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
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)
-
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)
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
-
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)
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
-
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!
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
-
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!
-
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!
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
-
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