473,895 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Troubles with exception::what ()

The following simple program behaves differently
in Windows and Linux .

#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;
class LogicError : public logic_error {
public:
string desc;
explicit LogicError(stri ng desc)
: logic_error("pa rent description"), desc(desc) {
}
virtual const char * what() const throw() {
return desc.c_str();
}
};
void f() {
throw LogicError("chi ld description");
}
int main() {
try {
f();
} catch(exception e) {
cout << "e.what=[" << e.what() << "]" << endl;
}
try {
f();
} catch(exception & e) {
cout << "e.what=[" << e.what() << "]" << endl;
}
return 0;
}

When I run it on Windows (Visual Studio 6.0) I obtain:
e.what=[]
e.what=[child description]

When I run it on Windows (Visual Studio 7.1) I obtain:
e.what=[Unknown exception]
e.what=[child description]

On Linux I (obtain:
e.what=[9exception]
e.what=[child description]

Two questions to the C++ gurus:
<1> Which is the expected ANSI-C++ behaviour?
<2> Why. in each run, the first output row
is different from the second one?

Thanks for the help.

- Dario

Jul 19 '05 #1
12 11200
Dario wrote:
The following simple program behaves differently
in Windows and Linux .
#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;
class LogicError : public logic_error {
public:
string desc;
explicit LogicError(stri ng desc)
: logic_error("pa rent description"), desc(desc) {
}
virtual const char * what() const throw() {
return desc.c_str();
}
This is overriding the base "what" method.

Incidently, gcc 3.3.1 needed this:

~LogicError() throw() {};

It seems to make sense since the destructor of the base class also
specified throw().
};
void f() {
throw LogicError("chi ld description");
}
int main() {
try {
f();
} catch(exception e) {
Exception caught by value - a copy of "exception" is made here.

Well this means that you're not using the method what above.

cout << "e.what=[" << e.what() << "]" << endl;
}
try {
f();
} catch(exception & e) {
Exception caught by reference - cool, now you will get the right what
becuase you are actually using a LogicError object and not an
"exception" object copied.
cout << "e.what=[" << e.what() << "]" << endl;
}
return 0;
}

When I run it on Windows (Visual Studio 6.0) I obtain:
e.what=[]
e.what=[child description]

When I run it on Windows (Visual Studio 7.1) I obtain:
e.what=[Unknown exception]
e.what=[child description]

On Linux I (obtain:
e.what=[9exception]
e.what=[child description]

Two questions to the C++ gurus:
<1> Which is the expected ANSI-C++ behaviour?
All of them. What happens on the first catch is undefined.
<2> Why. in each run, the first output row
is different from the second one?
Because they are different objects.

Thanks for the help.


Jul 19 '05 #2
On 26 Sep 2003 04:47:25 GMT, Gianni Mariani <gi*******@mari ani.ws>
wrote:
Dario wrote:
The following simple program behaves differently
in Windows and Linux .
#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;
BTW, one shouldn't ever put "using namespace ...<anything>" in a
header file... for toy programs such as this, it is probably OK.
class LogicError : public logic_error {
public:
string desc;
explicit LogicError(stri ng desc)
: logic_error("pa rent description"), desc(desc) {
}
virtual const char * what() const throw() {
return desc.c_str();
}


He also should have a *virtual* destructor. It might be a better idea
to derive the class from std::exception and not from one of its
derived classes because not every implementation of the STL uses a
virtual destructor in the derived classes. In some of them, it will
work; in others, maybe not.

This is overriding the base "what" method.

Incidently, gcc 3.3.1 needed this:

~LogicError() throw() {};

It seems to make sense since the destructor of the base class also
specified throw().
};
void f() {
throw LogicError("chi ld description");
}
int main() {
try {
f();
} catch(exception e) {


Exception caught by value - a copy of "exception" is made here.

Well this means that you're not using the method what above.

cout << "e.what=[" << e.what() << "]" << endl;
}
try {
f();
} catch(exception & e) {


Exception caught by reference - cool, now you will get the right what
becuase you are actually using a LogicError object and not an
"exception" object copied.
cout << "e.what=[" << e.what() << "]" << endl;
}
return 0;
}

When I run it on Windows (Visual Studio 6.0) I obtain:
e.what=[]
e.what=[child description]

When I run it on Windows (Visual Studio 7.1) I obtain:
e.what=[Unknown exception]
e.what=[child description]

On Linux I (obtain:
e.what=[9exception]
e.what=[child description]

Two questions to the C++ gurus:
<1> Which is the expected ANSI-C++ behaviour?


All of them. What happens on the first catch is undefined.
<2> Why. in each run, the first output row
is different from the second one?


Because they are different objects.

Thanks for the help.


--
Bob Hairgrove
rh************* *@Pleasebigfoot .com
Jul 19 '05 #3
In article <3f************ **@news.webshut tle.ch>,
rh************* ****@EMailAddre ssbigfoot.com says...

[ ... ]
#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;

BTW, one shouldn't ever put "using namespace ...<anything>" in a
header file...


....at namespace scope. Something like this:

#include <iostream>

class X {
using namespace std;

// declarations including "ostream" instead of "std::ostre am".
};

is perfectly reasonable. The using declaration follows scope rules, so
after the end of the declaration of 'X', the using declaration no longer
has any effect.

[ ... ]
He also should have a *virtual* destructor.
He does. He's deriving indirectly from class exception, which is
required to have a virtual destructor ($18.6.1). Since its destructor
is virtual, the destructors of all derived classes are also virtual
($12.4/7). Since he hasn't declared a dtor explicitly, it's declared
implicitly ($12.4/3). Since he hasn't defined it explicitly either, it
will be defined implicitly ($12.4/5).
It might be a better idea
to derive the class from std::exception and not from one of its
derived classes because not every implementation of the STL uses a
virtual destructor in the derived classes. In some of them, it will
work; in others, maybe not.


If the base class destructor is virtual, the destructors of _all_
derived classes are virtual. This is an absolute requirement of the
language, and does not depend on the implementation of the standard
library -- any compiler that makes a derived dtor non-virtual when the
base dtor is virtual is simply _horribly_ broken -- to the point that I
don't think it's worth discussing in the context of C++ at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #4

"Dario" <da***@despamme d.com> wrote in message news:bk******** **@fata.cs.inte rbusiness.it...
} catch(exception e) {
cout << "e.what=[" << e.what() << "]" << endl;
}
When I run it on Windows (Visual Studio 6.0) I obtain:

e.what=[]
e.what=[child description]

When I run it on Windows (Visual Studio 7.1) I obtain:
e.what=[Unknown exception]
e.what=[child description]


This is to be expected. In the first try/catch you have slice your
thrown exception into an "exception" . The dynamic type and
static type of e are the same. So in this case whatever implementation
defined string exception::what () prints is what you get.

In your second try/catch, you catch byreferece, therefore e still has
it's dynamic type of LogicError. The virtual what() function then
runs LogicError::wha t().
Jul 19 '05 #5

"Bob Hairgrove" <rh************ *****@EMailAddr essbigfoot.com> wrote in message news:3f******** ******@news.web shuttle.ch...

He also should have a *virtual* destructor.


The destructor is already virtual. std::exception' s destructor is virtual so all
of it's children also have virtual destructors.
Jul 19 '05 #6
The following simple program behaves differently
in Windows and Linux .

#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;
class LogicError : public logic_error {
public:
string desc;
explicit LogicError(stri ng desc)
: logic_error("pa rent description"), desc(desc) {
}
virtual const char * what() const throw() {
return desc.c_str();
}
};
void f() {
throw LogicError("chi ld description");
}
int main() {
try {
f();
} catch(exception e) {
cout << "e.what=[" << e.what() << "]" << endl;
}
try {
f();
} catch(exception & e) {
cout << "e.what=[" << e.what() << "]" << endl;
}
return 0;
}

When I run it on Windows (Visual Studio 6.0) I obtain:
e.what=[]
e.what=[child description]

When I run it on Windows (Visual Studio 7.1) I obtain:
e.what=[Unknown exception]
e.what=[child description]

On Linux I (obtain:
e.what=[9exception]
e.what=[child description]

Two questions to the C++ gurus:
<1> Which is the expected ANSI-C++ behaviour?
<2> Why. in each run, the first output row
is different from the second one?

Thanks for the help.

- Dario


No-one is able to give me an help ?

- Dario

Jul 19 '05 #7
Dario wrote:

....
Thanks for the help.

- Dario

No-one is able to give me an help ?


There were posts from Ron N. and myself. What's the issue ?

Jul 19 '05 #8
Bob Hairgrove wrote:
BTW, one shouldn't ever put "using namespace ...<anything>" in a
header file... for toy programs such as this, it is probably OK.
I use "using namespace ...<anything>" only in my *.cpp files.
He also should have a *virtual* destructor.
Yes, in my "actual" code I have it.
It might be a better idea to derive the class from std::exception and not from one of its
derived classes because not every implementation of the STL uses a
virtual destructor in the derived classes. In some of them, it will
work; in others, maybe not.


But my LogicError is a logic_error and not a generic exception.
So I will continue to derive from logic_error.
Incidently, gcc 3.3.1 needed this:

~LogicError() throw() {};

It seems to make sense since the destructor of the base class also
specified throw().
OK.
void f() {
throw LogicError("chi ld description");
}
int main() {
try {
f();
} catch(exception e) {


Exception caught by value - a copy of "exception" is made here.

Well this means that you're not using the method what above.
cout << "e.what=[" << e.what() << "]" << endl;
}
try {
f();
} catch(exception & e) {


Exception caught by reference - cool, now you will get the right what
becuase you are actually using a LogicError object and not an
"exception" object copied.
OK.
Two questions to the C++ gurus:
<1> Which is the expected ANSI-C++ behaviour?


All of them. What happens on the first catch is undefined.
OK. Understood.
<2> Why. in each run, the first output row
is different from the second one?


Because they are different objects.


OK. Understood.

Thanks.

- Dario

Jul 19 '05 #9

"Dario" <da***@despamme d.com> wrote in message news:bk******** **@grillo.cs.in terbusiness.it. ..

No-one is able to give me an help ?

I thought I did. Maybe we misunderstood your question.
The program behaves as expected. When you convert a derived class (LogicError)
a base class (exception), the object you create is the base class (exception). It's
not polymorphic, it is the base class initialzed with pieces of your derived class.

The reason it behaves differently between Linux an WIndows, is that exception::what ()
returns diferent things on those machines. If you wrote:

int main() {
exception e;
cout << e.what() << endl;
}

and ran it on both machiens, you'd get the same thing you see for your first print in your
program. The standard just says the string is "implementa tion defined." In the case of
VC++ 6.0 it appears to just be an empty string. In VC++ 7 it is "unkonwn exception".
On Linux, it's "exception. " All of these are technically correct.

When you use a reference to the base class instead, you have a polymorphic reference
to the derived object with the static type of the base class. In this case, the virtual function
what() returns your "child description."

Does this help? If not, you'll have to ask a more detailed question and we'll be glad to
explain further.

-Ron
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2142
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the exe file it doesn't catch any errors? Does anyone know what my problem might be? Regards
24
2370
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no virtual funcitons have no rtti) That is, there is no way to do something like: try{ funct_from_3rd_party(); } catch(...){ std:err << extract_name() << std::endl; }
11
1795
by: Dave | last post by:
try { ... } catch (exception e) { cout << e.what() << endl; } In the code above, e is caught by value rather than polymorphically (assume
10
5009
by: linq936 | last post by:
Hi, I have many assert() call in my code, now I am considering to replace them with exception. The reason I want to do this change is that with the program going bigger and bigger, it is hard to test all the corner cases, and thus assert() does not work as good as before. The problem is if there are something really bad which was not captured by assert(), then in runtime, most likely the program will crash. This is the worst user...
3
2264
by: Tony Johansson | last post by:
Hello! When you allocate object dynamically which mean on the heap I find that a problem when using exception. What is the prefer method to handle this kind of problem. //Tony
2
8886
by: James Foreman | last post by:
I'm building a revenue management tool and this requires some understanding of how good my forecast accuracy is. Therefore I'm trying to collect the mean absolute percentage error (MAPE) on a set of predictions that I've been making; I have a table that stores the actual result that occurred each day, and another that stores the predictions that I've made on particular days for each day. (All these predictions are integers). My SQL is...
0
995
by: Larry Rebich | last post by:
I'm running a very simple web application and get this error. What do I need to change? ------------------------------------------ Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. ...
2
6998
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: parseMetadata.obj : error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(void)" (??0exception@std@@QAE@XZ) 2>libcpmt.lib(string.obj) : error LNK2001: unresolved external symbol "public: __thiscall...
2
2382
by: =?Utf-8?B?QW5kcmUgS2xpbW92?= | last post by:
Hi! Situation: simple code read registry value (OpenSubKey -GetValue, as usual). Requested value exists in registry and has type DWORD, read into the variable UInt32, access rights granted. When value's data is in interval &H0 - &H7FFFFFFF - it's works fine. When value's data has "sign bit rised" (&HF0000000 - &HFFFFFFFF) - arithmetic
0
11249
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10847
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10937
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10473
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9651
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7179
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3298
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.