473,587 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ 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>.cp p -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 1901
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>.cp p -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( goldbachConject ureIsTrue() )
{
return 1234;
}
}

The compiler has no way to know whether goldbachConject ureIsTrue()
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**********@e xcite.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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>.cp p -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
5698
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 connections and executescalar work fine, but, I want to put it into a Try Catch statement so that if a problem occurs, it will error out to my error...
13
3700
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
6
5041
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 regular defensive programming? (ie if file exists do this, if not do something else) Or should Try + Catch be used TO SUPPLAMENT regular defensive...
13
1568
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, Description from JnlType", mySqlConnection) Dim Ds As New DataSet("X")
20
1356
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 block of code below that is part of a function. Should it be at the end of the try block, as dipicted below, or should it be after the End Try Block? ...
4
1946
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> int main() { int * parser;
3
15416
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 following error if the xmlField 'location' doesn't exist in the xmlfile or if the xmlfile is blank?
28
3792
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
3
2550
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 denominator\n"; void inverse(long value, double& answer) {
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.