473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1761
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.********@com Acast.net> wrote in message
news:2V******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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.********@com Acast.net> wrote in message
news:2V******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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.********@com Acast.net> wrote in message
news:br******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
Dave wrote:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:2V******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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_erro r, 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_erro r, 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.********@com Acast.net> wrote in message
news:rq******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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_erro r, 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.supe rnews.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

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

Similar topics

2
1756
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. However, in the documentation it seems like there is not a way to get the extra str data if you are handling all exceptions and not specifically. Is...
8
1727
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 Base_error("An object of incorrect type was given....");
1
1517
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 exception in multi dll application ! I have reproduced this bug in the very simple application which consists of 2 dll and 1 exe file. Its behaviour...
2
1800
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 exception } }
7
2322
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 of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log...
12
6093
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 out of which unmanaged exception //get thrown
7
1534
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 _getx(self): return self._x def _setx(self,strvalue): try: self._x = float(strvalue)
12
18260
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
3275
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 the correct inputs for filetype #------------------------------- def readGrid( self, coord='xyz' ): mg = ( '.FALSE.', '.TRUE.' ) form = (...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
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
8130
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
7677
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
7979
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
6284
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...
0
5219
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...
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.