By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

catch with no return value....

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.