469,898 Members | 1,547 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

return from catch - UB?

The following code does VERY strange things in the debugger. Is there
anything wrong with the following code, UB or anything else?

#include <iostream>
#include <exception>
#include <stdexcept>

int main()
{
int * parser;

try
{
parser = 0;
}
catch (std::out_of_range & err)
{
}
catch (std::bad_alloc & err)
{
return 0; // debugger jumps to here after hitting final brace -
"runs" this statement as many times as there are returns in catches.
}
catch (std::domain_error & err)
{
return -1;
}
catch (std::exception & err)
{
std::cerr << err.what() << std::endl;
}
catch(...)
{
return -1;
} // debugger jumps to here after running code in try..
return 0; // debugger finally jumps to here and leaves...other code in
this area will run but has been removed for brevity.
}
Granted, it isn't a normal thing to do, but AFAICT the standard
explicitly allows it.

Jun 8 '06 #1
4 1725
Noah Roberts wrote:
The following code does VERY strange things in the debugger. Is there
anything wrong with the following code, UB or anything else?

#include <iostream>
#include <exception>
#include <stdexcept>

int main()
{
int * parser;

try
{
parser = 0;
}
catch (std::out_of_range & err)
{
}
catch (std::bad_alloc & err)
{
return 0; // debugger jumps to here after hitting final brace -
"runs" this statement as many times as there are returns in catches.
}
catch (std::domain_error & err)
{
return -1;
}
catch (std::exception & err)
{
std::cerr << err.what() << std::endl;
}
catch(...)
{
return -1;
} // debugger jumps to here after running code in try..
return 0; // debugger finally jumps to here and leaves...other code in
this area will run but has been removed for brevity.
}
Granted, it isn't a normal thing to do, but AFAICT the standard
explicitly allows it.


Have you tried turning off all optimizations? This sounds exactly like
the sort of weird behavior you'd expect to see when trying to debug
optimized code.

--
Alan Johnson
Jun 8 '06 #2
Noah Roberts wrote:
catch (std::bad_alloc & err)
{
return 0; // debugger jumps to here after hitting final brace -
"runs" this statement as many times as there are returns in catches.
}

return 0; // debugger finally jumps to here and leaves...other code in
this area will run but has been removed for brevity.
}


Sounds like the second 'return 0' has been optimised to
jump to the earlier return 0, instead of repeating the code.

Jun 9 '06 #3

Old Wolf wrote:
Noah Roberts wrote:
catch (std::bad_alloc & err)
{
return 0; // debugger jumps to here after hitting final brace -
"runs" this statement as many times as there are returns in catches.
}

return 0; // debugger finally jumps to here and leaves...other code in
this area will run but has been removed for brevity.
}


Sounds like the second 'return 0' has been optimised to
jump to the earlier return 0, instead of repeating the code.


Ok, I am not sure if there are optimizations turned on, I'll check when
I get to work tomarro. However, even if the first return 0 is a -1 it
goes there. Also, when code lies between the last catch brace and the
return statement the debugger jumps to the last brace, then to the
first return in a catch and "runs" it once per return statement in a
catch (return value irrelivant) and then jumps to finish the code after
the try/catch and finally goes to the return at the end of the
function. Interestingly even though it jumps to the first return and
acts like it is stepping for each return in the try/catch, it doesn't
actually return....it loops through it and then acts like it never did
that.

Frankly the behavior makes no sense to me at all...

I will check ops though and see if it goes away. We were discussing
this and I am of the conclusion that so long as it doesn't execute any
extra code in the catch clauses, and it doesn't, and then executes all
code after the catch clauses and returns as if it never did these odd
things...well, then it follows the "as if" clause of the standard...no
matter how off the wall its true behavior seems it actually does do
what it is supposed to. Is it accurate to say it still complies?

Jun 9 '06 #4

Noah Roberts wrote:
Old Wolf wrote:
Noah Roberts wrote:
catch (std::bad_alloc & err)
{
return 0; // debugger jumps to here after hitting final brace -
"runs" this statement as many times as there are returns in catches.
}

return 0; // debugger finally jumps to here and leaves...other code in
this area will run but has been removed for brevity.
}


Sounds like the second 'return 0' has been optimised to
jump to the earlier return 0, instead of repeating the code.


Ok, I am not sure if there are optimizations turned on, I'll check when
I get to work tomarro.


Yeah, optimizations are totally disabled afaict. Strange stuff.

Jun 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by adi | last post: by
6 posts views Thread by George | last post: by
8 posts views Thread by Daniel Billingsley | last post: by
3 posts views Thread by Eric the half a Bee | last post: by
17 posts views Thread by hplloyd | last post: by
14 posts views Thread by dcassar | last post: by
3 posts views Thread by Doug | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.