Connecting Tech Pros Worldwide Forums | Help | Site Map

catch with no return value....

George
Guest
 
Posts: n/a
#1: Jul 23 '05
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;
}


Larry I Smith
Guest
 
Posts: n/a
#2: Jul 23 '05

re: catch with no return value....


George wrote:[color=blue]
> 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;
> }[/color]


return a;
[color=blue]
> }
> int foo2 ()
> {
> int x = 45;
> try {
> foo1();
> return x;
> } catch (...) {
> cout <<"Unknown exception in foo2()" << endl;
> }
>[/color]

return x;
[color=blue]
> }
>
> int main()
> {
> int x = foo2();
> cout << " x = " << x << endl;
> cout << "end" << endl;
> return 0;
> }
>[/color]
Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 23 '05

re: catch with no return value....


* George:[color=blue]
>
> I thought this would not compile because no return value is specified.
> But it does compile and run (aix and xlc v7.0.)[/color]

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?
Rolf Magnus
Guest
 
Posts: n/a
#4: Jul 23 '05

re: catch with no return value....


Larry I Smith wrote:
[color=blue][color=green]
>> I thought this would not compile because no return value is specified.[/color][/color]

....
[color=blue]
> return a;[/color]

....
[color=blue]
> return x;[/color]


Have you actually read the question?

Larry I Smith
Guest
 
Posts: n/a
#5: Jul 23 '05

re: catch with no return value....


Rolf Magnus wrote:[color=blue]
> Larry I Smith wrote:
>[color=green][color=darkred]
>>>I thought this would not compile because no return value is specified.[/color][/color]
>
> ...
>[color=green]
>> return a;[/color]
>
> ...
>[color=green]
>> return x;[/color]
>
>
> Have you actually read the question?
>[/color]

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

Larry
Jim Langston
Guest
 
Posts: n/a
#6: Jul 23 '05

re: catch with no return value....


"George" <george.cross@excite.com> wrote in message
news:1119747279.490182.102560@f14g2000cwb.googlegr oups.com...[color=blue]
> 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;
> }
>[/color]

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.


Rolf Magnus
Guest
 
Posts: n/a
#7: Jul 23 '05

re: catch with no return value....


Larry I Smith wrote:
[color=blue]
> Rolf Magnus wrote:[color=green]
>> Larry I Smith wrote:
>>[color=darkred]
>>>>I thought this would not compile because no return value is specified.[/color]
>>
>> ...
>>[color=darkred]
>>> return a;[/color]
>>
>> ...
>>[color=darkred]
>>> return x;[/color]
>>
>>
>> Have you actually read the question?
>>[/color]
>
> Yes, but I didn't understand what he was asking for.
> I still don't.[/color]

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

Closed Thread