"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.