473,419 Members | 4,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Catching class exception

try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume
that what was thrown is derived from exception). What does the Standard say
about whether or not the call to e.what() will do the right thing? Is it
guaranteed to work, guaranteed to not work or implementation defined?

Thanks,
Dave
Jul 23 '05 #1
11 1740
Dave wrote:
try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume
that what was thrown is derived from exception). What does the Standard say
about whether or not the call to e.what() will do the right thing?
What do you mean by "the right thing"?
Is it
guaranteed to work, guaranteed to not work or implementation defined?


The 'what' member function returns some valid pointer. Displaying it
is what it's there for. What specifically it will return is defined
by the implementation. If you catch the base class like this, it will
return something "suitable for conversion and display", but not
necessarily meaningful. The code will definitely not call the derived
class' "what()" member.

V
Jul 23 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2V*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume that what was thrown is derived from exception). What does the Standard say about whether or not the call to e.what() will do the right thing?
What do you mean by "the right thing"?


By "the right thing", I'm asking if it will return a pointer to the string
that was used to construct the *derived* object as opposed to a pointer to
some other string. Is it guaranteed to work, guaranteed to not work or
implementation defined given that the base what() is being called?
> Is it
guaranteed to work, guaranteed to not work or implementation defined?


The 'what' member function returns some valid pointer. Displaying it
is what it's there for. What specifically it will return is defined
by the implementation. If you catch the base class like this, it will
return something "suitable for conversion and display", but not
necessarily meaningful. The code will definitely not call the derived
class' "what()" member.

V

Jul 23 '05 #3
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2V*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically
(assume
that what was thrown is derived from exception). What does the Standard
say
about whether or not the call to e.what() will do the right thing?
What do you mean by "the right thing"?

By "the right thing", I'm asking if it will return a pointer to the string
that was used to construct the *derived* object


What derived object? What string passed to construct it? std::exception
has *no idea* about them.
as opposed to a pointer to
some other string.
Of course it's a pointer to some other string. 'std::exception' has no
constructor that takes a C string or a C++ string.
Is it guaranteed to work, guaranteed to not work or
implementation defined given that the base what() is being called?


What guaranteed to work?

I write the class:

class Base {
public:
Base();
virtual ~Base();
virtual int getnumber() const { return 42; }
};

How in the world should *I* know that *you* derived from it and made
*your* class to be constructed with some other number, which *you*
intended to *your* overridden 'getnumber' to return?

Just think a bit before replying.

V
Jul 23 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:br*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2V*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:

try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically


(assume
that what was thrown is derived from exception). What does the
Standard
say
about whether or not the call to e.what() will do the right thing?

What do you mean by "the right thing"?

By "the right thing", I'm asking if it will return a pointer to the string that was used to construct the *derived* object


What derived object? What string passed to construct it? std::exception
has *no idea* about them.
> as opposed to a pointer to
some other string.


Of course it's a pointer to some other string. 'std::exception' has no
constructor that takes a C string or a C++ string.
> Is it guaranteed to work, guaranteed to not work or
implementation defined given that the base what() is being called?


What guaranteed to work?

I write the class:

class Base {
public:
Base();
virtual ~Base();
virtual int getnumber() const { return 42; }
};

How in the world should *I* know that *you* derived from it and made
*your* class to be constructed with some other number, which *you*
intended to *your* overridden 'getnumber' to return?

Just think a bit before replying.

V


OK, I guess I have to spell this out for you...

The definition of class std::exception contains the following declaration:

virtual const char* what() const throw();

Now take class std::logic_error, which derives publicly from class
exception, as an example.

class logic_error *must* be constructed with a std::string (for later use by
what()), class exception *may not* be constructed with a std::string. It
may only be default or copy constructed.

The question comes down to where the string that what() uses lives. It can
live either in class exception or it can live in the classes derived from
exception. Where the string lives determines what happens when I catch a
class exception by *value* and therefore end up calling the *base* what().

If the std::string lives in class exception (i.e. class exception holds the
string for the derived classes and they inherit it), the right thing will
print out upon using what(). If each derived class is responsible for
holding its own "what" string, the right thing will not print out upon using
what().

In my mind, the behavior is implementation defined, bu tI was hoping to get
confirmation of that.

Just think a bit before replying.
Jul 23 '05 #5
Dave wrote:
[...]
OK, I guess I have to spell this out for you...
:-)
The definition of class std::exception contains the following declaration:

virtual const char* what() const throw();

Now take class std::logic_error, which derives publicly from class
exception, as an example.

class logic_error *must* be constructed with a std::string (for later use by
what()),
"for later use" by WHOSE "what()"? It's a virtual function.
class exception *may not* be constructed with a std::string. It
may only be default or copy constructed.

The question comes down to where the string that what() uses lives. It can
live either in class exception or it can live in the classes derived from
exception. Where the string lives determines what happens when I catch a
class exception by *value* and therefore end up calling the *base* what().

If the std::string lives in class exception (i.e. class exception holds the
string for the derived classes and they inherit it), the right thing will
print out upon using what(). If each derived class is responsible for
holding its own "what" string, the right thing will not print out upon using
what().
Yes, you get it now!
In my mind, the behavior is implementation defined, bu tI was hoping to get
confirmation of that.
The Standard says NOTHING about all this, as I already told you.
Just think a bit before replying.


Get your own copy of the Standard and stop wasting our time.

V
Jul 23 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:rq*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
[...]
OK, I guess I have to spell this out for you...
:-)
The definition of class std::exception contains the following declaration:
virtual const char* what() const throw();

Now take class std::logic_error, which derives publicly from class
exception, as an example.

class logic_error *must* be constructed with a std::string (for later use by what()),


"for later use" by WHOSE "what()"? It's a virtual function.
> class exception *may not* be constructed with a std::string. It
may only be default or copy constructed.

The question comes down to where the string that what() uses lives. It can live either in class exception or it can live in the classes derived from exception. Where the string lives determines what happens when I catch a class exception by *value* and therefore end up calling the *base* what(). >
If the std::string lives in class exception (i.e. class exception holds the string for the derived classes and they inherit it), the right thing will print out upon using what(). If each derived class is responsible for
holding its own "what" string, the right thing will not print out upon using what().


Yes, you get it now!


I got it all along. You did not understand what I was asking.
In my mind, the behavior is implementation defined, bu tI was hoping to get confirmation of that.
The Standard says NOTHING about all this, as I already told you.
Just think a bit before replying.


Get your own copy of the Standard and stop wasting our time.


You are absolutely free to not respond to any question if you so choose.
Nobody has an obligation to provide an answer in this or any NG. Nobody can
waste your time but you.

V

Jul 23 '05 #7
I'm not sure if this is what you're asking, but Alexandrescu and Sutter
explain in "C++ Coding Standards" a rationale for throwing by value and
catching by reference.

HTH
"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.com...
try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume that what was thrown is derived from exception). What does the Standard say about whether or not the call to e.what() will do the right thing? Is it
guaranteed to work, guaranteed to not work or implementation defined?

Thanks,
Dave

Jul 23 '05 #8
Dave wrote:
try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume
that what was thrown is derived from exception). What does the Standard say
about whether or not the call to e.what() will do the right thing? Is it
guaranteed to work, guaranteed to not work or implementation defined?


This code will, of course, slice the exception object, i.e
'std::exception::what()' will be called, not the derived class'
'what()'. The string, pointer to which is returned by
'std::exception::what()', is implementation defined, which means that in
general case it will not "do the right thing".

In order to make it "do the right thing" the exception has to be caught
by reference.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #9
Victor Bazarov wrote:
Dave wrote:
"Victor Bazarov" wrote:
Dave wrote:

try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

What does the Standard say about whether or not the call
to e.what() will do the right thing? What do you mean by "the right thing"?

He obviously means, if the ... above contained 'throw
runtime_error("foo");', will "foo" be written to cout?
as opposed to a pointer to some other string.
Of course it's a pointer to some other string. 'std::exception'
has no constructor that takes a C string or a C++ string.


Non-sequitur. std::exception could contain a string internally,
even though it has no constructor that takes one.
> Is it guaranteed to work, guaranteed to not work or
implementation defined given that the base what() is being called?


What guaranteed to work?


The above code example !
I write the class:

class Base {
public:
Base();
virtual ~Base();
virtual int getnumber() const { return 42; }
};

How in the world should *I* know that *you* derived from
it and made *your* class to be constructed with some other
number, which *you* intended to *your* overridden
'getnumber' to return?
The question is, is that a valid implementation for std::exception?
It would be easy to implement std::exception in a way that
does 'work'.

Just think a bit before replying.

You can't solve that one by thinking, without reading the
Standard. And if he should follow your suggestion and
not bother this newsgroup with questions, perhaps we should
shut down the newsgroup entirely?

Jul 23 '05 #10
Old Wolf wrote:
[...]
You can't solve that one by thinking, without reading the
Standard. And if he should follow your suggestion and
not bother this newsgroup with questions, perhaps we should
shut down the newsgroup entirely?

Look, the Standard is available, and we're here not to re-type or
copy/paste it into our answers. That's one. Second, I am fairly
certain that even in the absence of the Standard, the OP has some
sort of documentation describing the behaviour of the 'std::exception'
class, which undoubtedly says something about this. That's two.
Third, there is other documentation readily available on the Web,
which probably describes this just as well. If that all means we
need to shut down the newsgroup, then we need to shut it down.
*I* don't think so. But the OP's question was not about something
that cannot be figured out without the newsgroup.

And I still think that questions, answers to which can be found
elsewhere, should only be asked here after a bit of thinking. That
is what I suggested. And get off my case on this.

Now,
The question is, is that a valid implementation for std::exception?
Or course it is. The Standard says nothing about what 'what' should
return except that it's an NTBS suitable for conversion and display.
It would be easy to implement std::exception in a way that
does 'work'.


That is totally irrelevant.
Jul 23 '05 #11
> Old Wolf wrote:
[...]
You can't solve that one by thinking, without reading the
Standard. And if he should follow your suggestion and
not bother this newsgroup with questions, perhaps we should
shut down the newsgroup entirely?

Look, the Standard is available, and we're here not to re-type or
copy/paste it into our answers. That's one. Second, I am fairly
certain that even in the absence of the Standard, the OP has some
sort of documentation describing the behaviour of the

'std::exception' class, which undoubtedly says something about this. That's two.
Third, there is other documentation readily available on the Web,
which probably describes this just as well. If that all means we
need to shut down the newsgroup, then we need to shut it down.
*I* don't think so. But the OP's question was not about something
that cannot be figured out without the newsgroup.

And I still think that questions, answers to which can be found
elsewhere, should only be asked here after a bit of thinking. That
is what I suggested. And get off my case on this.

That may all be true, but you don't have to be a jerk! The Standard
may not be understandable, or the OP may not have access to the
Standard, or any number of other reasons may exist why they're posting
the question to this newsgroup.

Now,
> The question is, is that a valid implementation for
std::exception?
Or course it is. The Standard says nothing about what 'what' should
return except that it's an NTBS suitable for conversion and display.
> It would be easy to implement std::exception in a way that
> does 'work'.


That is totally irrelevant.


I could be completely wrong, but I believe the answer the OP is looking
for is that he needs to catch by reference so that the call to what()
behaves polymorphically instead of slicing the thrown object down to
std::exception.

Jul 23 '05 #12

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

Similar topics

2
by: Keith Bolton | last post by:
I am handling exceptions currently using try, except. Generally I don't handle specific exceptions and am catching all. Then if an exception occurs, I would like to capture that error string....
8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
1
by: vlar | last post by:
01/30/2004 I have posted "Serious bug in ngen.exe" in this thread but so far (in .NET 1.1 SP1) this bug is not yet fixed !!! So I am repeating this post again: Ngen stops catching of derived...
2
by: Adam | last post by:
If I catch an exception in a static callback method like: private static void ResponseCallback(IAsnycResult asyncResult) { try { //code...code...code } catch (WebException e) { //handle...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
7
by: jm.suresh | last post by:
Hi, In the following program, I have a class Test which has a property x. Its setx function gets a string value and converts it into a float and stores into it. class Test(object): def...
12
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.