472,328 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 1825
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had...
6
by: George | last post by:
Hi All, I thought this would not compile because no return value is specified. But it does compile and run (aix and xlc v7.0.) Could someone...
8
by: Daniel Billingsley | last post by:
Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch...
3
by: Eric the half a Bee | last post by:
Hello I am trying to implement a search function within a collection of Employees. I am searching for a specific EmpID number in the...
17
by: hplloyd | last post by:
Hi, I have a function that adds data to a database and I want to return true if the database was updated and false if there was a problem, so I...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. ...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those...
3
by: Doug | last post by:
Hi i have a method that returns a value public bool readxml (string xmlFilename, out string value) but I would like to catch an exception if it...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.