catch with no return value....

G

George

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;
}
 
L

Larry I Smith

George said:
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;
 
A

Alf P. Steinbach

* 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.
 
J

Jim Langston

George said:
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.
 
R

Rolf Magnus

Larry said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top