473,779 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ra nge & 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_er ror & 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 1955
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_ra nge & 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_er ror & 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
2888
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
1915
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 would be covered? Compiler Output is: '(W) A return value of type "int" is expected.' Runtime Output is:
8
1859
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 return a value, so... do I just put return null; after I throw my exception in the catch block? or should I initialize the local variable to null and return it in a finally block?
3
5877
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 Employee. This I can do. The problem comes if the EmpID is not found in the collection. The only way to achieve this I can think of is to throw an invalid argument exception:
17
1961
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 that I can return true ok is the routine succeeds but where do I put the return false; statement id the process fails - I get a compile error say unreachable code with whatever i try...
14
3482
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 public member with a return type that is derived from System.Exception? I understand the importance of having clean, concise code that follows widely-accepted patterns and practices, but in this case, I find it hard to blindly follow a standard...
22
4044
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 receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to EndReceive return zero bytes length , also the buffer is empty. here is the code: public static byte...
24
2198
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 the UI layer to a method in a library class that calls the stored procedure. Best to return a boolean indicating success/failure, return a string with the exception message, or just return the entire exception?
3
15432
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 following error if the xmlField 'location' doesn't exist in the xmlfile or if the xmlfile is blank?
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.