473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 1934
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 created a COM object (using VB6), which uses...
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 kindly please point me to where in the spec this...
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 one. The compiler insists that all code paths...
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 collection, and if it is found, I want to return 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 am using a try... catch block... My problem is...
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. Why is it considered bad practice to define a...
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 somepoint (same location in the code) when I want to...
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 classes. For example, let's say I send an update from...
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 occurs in the method . How do i catch the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.