473,471 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

catch with no return value....

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:

../a.out
Unknown exception
Unknown exception in foo2()
x = 804397472;
end

Here is the code:
//compile with xlc -qnooptimize -o <exe> <thisfile>.cpp -lc -lC
#include <iostream>
#include <exception>
#include <memory>
using namespace std;

struct A {
char* p;
};

auto_ptr<A> foo1() throw (exception)
{
auto_ptr<A> a(new A());
a->p = "filter this";
try{
if (2 > 1 ){
throw exception();
}else{
return a;
}
}
catch (...) {
cout << "Unknown exception" << endl ;
throw;
}
}
int foo2 ()
{
int x = 45;
try {
foo1();
return x;
} catch (...) {
cout <<"Unknown exception in foo2()" << endl;
}

}

int main()
{
int x = foo2();
cout << " x = " << x << endl;
cout << "end" << endl;
return 0;
}

Jul 23 '05 #1
6 1891
George wrote:
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:

./a.out
Unknown exception
Unknown exception in foo2()
x = 804397472;
end

Here is the code:
//compile with xlc -qnooptimize -o <exe> <thisfile>.cpp -lc -lC
#include <iostream>
#include <exception>
#include <memory>
using namespace std;

struct A {
char* p;
};

auto_ptr<A> foo1() throw (exception)
{
auto_ptr<A> a(new A());
a->p = "filter this";
try{
if (2 > 1 ){
throw exception();
}else{
return a;
}
}
catch (...) {
cout << "Unknown exception" << endl ;
throw;
}

return a;
}
int foo2 ()
{
int x = 45;
try {
foo1();
return x;
} catch (...) {
cout <<"Unknown exception in foo2()" << endl;
}

return x;
}

int main()
{
int x = foo2();
cout << " x = " << x << endl;
cout << "end" << endl;
return 0;
}

Jul 23 '05 #2
* George:

I thought this would not compile because no return value is specified.
But it does compile and run (aix and xlc v7.0.)


There is a return value specified in one branch of the function logic.

Consider

int foo()
{
if( goldbachConjectureIsTrue() )
{
return 1234;
}
}

The compiler has no way to know whether goldbachConjectureIsTrue()
will return logical true or false. It assumes you know what you're
doing, that it always will return true. A good compiler may, however,
warn about this, and yours did.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
Larry I Smith wrote:
I thought this would not compile because no return value is specified.

....
return a;
....
return x;

Have you actually read the question?

Jul 23 '05 #4
Rolf Magnus wrote:
Larry I Smith wrote:
I thought this would not compile because no return value is specified.


...
return a;


...
return x;

Have you actually read the question?


Yes, but I didn't understand what he was asking for.
I still don't.

Larry
Jul 23 '05 #5
"George" <ge**********@excite.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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:

./a.out
Unknown exception
Unknown exception in foo2()
x = 804397472;
end

Here is the code:
//compile with xlc -qnooptimize -o <exe> <thisfile>.cpp -lc -lC
#include <iostream>
#include <exception>
#include <memory>
using namespace std;

struct A {
char* p;
};

auto_ptr<A> foo1() throw (exception)
{
auto_ptr<A> a(new A());
a->p = "filter this";
try{
if (2 > 1 ){
throw exception();
}else{
return a;
}
}
catch (...) {
cout << "Unknown exception" << endl ;
throw;
}
}
int foo2 ()
{
int x = 45;
try {
foo1();
return x;
} catch (...) {
cout <<"Unknown exception in foo2()" << endl;
}

}

int main()
{
int x = foo2();
cout << " x = " << x << endl;
cout << "end" << endl;
return 0;
}


Catch blocks are only executed if there is an exception thrown.
Catch blocks are supposed to take care of the code and try
to gracefully resume. I see what you're saying though.

In:
int foo2 ()
{
int x = 45;
try
{
foo1();
return x;
} catch (...)
{
cout <<"Unknown exception in foo2()" << endl;
}
}

If foo1() throws an exception, your catch block will execute,
and since it only does a cout it will flow to the bottom of the
function and... exit the function without a return statement.

This will produce UB I"m sure, an possibly crash your system
if the compiler tries to remove a return value from the stack
that was never put there in the first place.

I understand why you think it wouldn't compile, and in a
perfect world the compiler would check for what you are
doing in a catch statement, but I believe the compiler
presumes you know what you're doing in try...catch
blocks and doesn't do "normal" sanity checks.

Not sure if this is standard or not. think I'm going to play
around with this and see if what type of UB happens on
my compiler.
Jul 23 '05 #6
Larry I Smith wrote:
Rolf Magnus wrote:
Larry I Smith wrote:
I thought this would not compile because no return value is specified.


...
return a;


...
return x;

Have you actually read the question?


Yes, but I didn't understand what he was asking for.
I still don't.


He was asking why his code - even though the return statements were
missing - was accepted by the compiler.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bungle | last post by:
Hi I am trying to do something really simple. Create a method within a class which will connect to the database, pull back a result and return it out the method. Not hard. All my database...
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
13
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
20
by: Woody Splawn | last post by:
In a message yesterday titled Try Catch Question I got numerous responses. Thank you to all. After all the smoke clears I guess the question is where to place the Return True statement in the...
4
by: Noah Roberts | last post by:
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>...
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...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
3
by: aemado | last post by:
I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions? #include <iostream> using namespace std; string msgZero = "Zero...
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
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,...
1
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...
0
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...
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.