473,657 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exception and polymorphism problem

Hi I have this code:

class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }

private:
string msg;
};

int main()
{
try
{
throw Exception();
}
catch(exception e)
{
cout << e.what() << endl;
}

return 0;
}

when I run it I don't have print out my message "exception! " but
St9exception

why?

May 16 '07 #1
12 2173
On 16 Maj, 13:01, josh <xdevel1...@gma il.comwrote:
Hi I have this code:

class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }

private:
string msg;

};

int main()
{
try
{
throw Exception();
}
catch(exception e)
{
cout << e.what() << endl;
}

return 0;

}

when I run it I don't have print out my message "exception! " but
St9exception
Because you catch the exception by value, so you copy-construct a new
std::exception from the Exception that was thrown, catch the exception
by reference instead and it will work:

catch (exception& e)

--
Erik Wikström

May 16 '07 #2
On 5ÔÂ16ÈÕ, ÏÂÎç7ʱ01·Ö, josh <xdevel1...@gma il.comwrote:
Hi I have this code:

class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }

private:
string msg;

};

int main()
{
try
{
throw Exception();
An exception object is always created at the throw point.We reference
it as "temp" here.
}
catch(exception e)
The exception declaration of a catch clause behaves very much like a
parameter declaration. Here, e is another object of type exception
(Not Exception) that initialized by the "temp" of type Exception.
{
cout << e.what() << endl;
So, here, the e.what() refers to the what() member function of class
Exception, but not class exception.
}

return 0;

}

when I run it I don't have print out my message "exception! " but
St9exception

why?

May 16 '07 #3
On 16 Mag, 13:04, Erik Wikström <eri...@student .chalmers.sewro te:
On 16 Maj, 13:01, josh <xdevel1...@gma il.comwrote:
Hi I have this code:
class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }
private:
string msg;
};
int main()
{
try
{
throw Exception();
}
catch(exception e)
{
cout << e.what() << endl;
}
return 0;
}
when I run it I don't have print out my message "exception! " but
St9exception

Because you catch the exception by value, so you copy-construct a new
std::exception from the Exception that was thrown, catch the exception
by reference instead and it will work:

catch (exception& e)

--
Erik Wikström
yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?

P.S.
I try to read the source of exception.h and exception.cpp but in my
gcc source I didn't find it!
where are them?

May 16 '07 #4
josh wrote:
>
yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?
no, you did. the problem, as Eric said, is that if you don't put the
reference a new std::exception is created by copy-constructor. And for
the standard exception the what behaves differently that for your class.
P.S.
I try to read the source of exception.h and exception.cpp but in my
gcc source I didn't find it!
where are them?
it depends. For me, /usr/include/c++/4.1.2/exception

Regards,

Zeppe
May 16 '07 #5
On 16 Mag, 13:49, Zeppe <zeppe@.remove. all.this.long.c omment.email.it >
wrote:
josh wrote:
yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?

no, you did. the problem, as Eric said, is that if you don't put the
reference a new std::exception is created by copy-constructor. And for
the standard exception the what behaves differently that for your class.
P.S.
I try to read the source of exception.h and exception.cpp but in my
gcc source I didn't find it!
where are them?

it depends. For me, /usr/include/c++/4.1.2/exception

Regards,

Zeppe
I do it but still the code doesn't work.

....

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

here e is a reference to the new Exception() created so
at run-time e.what() should point to my Exception class
and to print "exception! " but it doesn't do that and print
9Exception ... why?
May 16 '07 #6
Hi

Zeppe wrote:
josh wrote:
>yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?

no, you did. the problem, as Eric said, is that if you don't put the
reference a new std::exception is created by copy-constructor. And for
the standard exception the what behaves differently that for your class.
No, he did not. In std::exception it is
virtual const char* what() const throw();

So it should read:

class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() const throw () { return msg.c_str(); }

private:
string msg;
};

The const is part of the function signature and thus necessary for
overriding while without the throw() part the program would be ill-formed.

Markus
May 16 '07 #7
Markus Moll wrote:
Hi

Zeppe wrote:
>josh wrote:
>>yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?

no, you did. the problem, as Eric said, is that if you don't put the
reference a new std::exception is created by copy-constructor. And for
the standard exception the what behaves differently that for your class.

No, he did not.
Damn lunch break! I know I should have checked before sending, but I was
hungry... :)

Sorry guys

Regards,

Zeppe
May 16 '07 #8
On 16 Mag, 14:05, Markus Moll <markus.m...@es at.kuleuven.bew rote:
Hi

Zeppe wrote:
josh wrote:
yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?
no, you did. the problem, as Eric said, is that if you don't put the
reference a new std::exception is created by copy-constructor. And for
the standard exception the what behaves differently that for your class.

No, he did not. In std::exception it is
virtual const char* what() const throw();

So it should read:

class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() const throw () { return msg.c_str(); }

private:
string msg;

};

The const is part of the function signature and thus necessary for
overriding while without the throw() part the program would be ill-formed.

Markus
yes that was my wrong (and also to make Exception not a reference) now
it works!!!

May 16 '07 #9
On May 16, 1:04 pm, Erik Wikström <eri...@student .chalmers.sewro te:
On 16 Maj, 13:01, josh <xdevel1...@gma il.comwrote:
Hi I have this code:
class Exception : public exception
{
public:
Exception(strin g m="exception! ") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }
private:
string msg;
};
int main()
{
try
{
throw Exception();
}
catch(exception e)
{
cout << e.what() << endl;
}
return 0;
}
when I run it I don't have print out my message "exception! " but
St9exception
Because you catch the exception by value, so you copy-construct a new
std::exception from the Exception that was thrown, catch the exception
by reference instead and it will work:
catch (exception& e)
Just a detail, but he's probably not going to be modifying the
object, so:
catch ( exception const& e )
would be more appropriate. (In practice, in the case of
exceptions, I don't think it makes much difference, but using
const systematically when appropriate is a good habit to get
into.)

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 16 '07 #10

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

Similar topics

3
2133
by: Mayer Goldberg | last post by:
Can someone please explain the motivation behind the following restriction in the language: I define an interface and two classes implementing it: public interface InterA {} public class B implements InterA {} public class C implements InterA {}
18
12578
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion about whether we should allow polymorphism. Our system is not embedded and does not need to be as real-time as, say, a pacemaker. But it does involve updating displays based on radar input. So a need for something close to real-time is desired...
3
3684
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
4
2396
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
16
6576
by: ChInKPoInt [No MCSD] | last post by:
I am using Visual Studio 2K3 writing a ASP.NET web application. Is there a way to force the C# compiler to catch possible exception? In Java, all exceptions thrown MUST BE caught, otherwise compiler would give you error. In C#, is there a way to do that?
13
2623
by: junw2000 | last post by:
Is C++ Exception handling useful? think it is too complicated. What kinds of project need to use it? Thanks.
11
2963
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
1
3272
by: usenet | last post by:
I wrote some sample code (see below) for nested exception throwing i.e. my catch blocks are throwing exceptions of their own (for simplicity I used standard exceptions). I am getting some extraneous characters (St9) in my final output. Can anyone kindly help me interpret that please? Thanks, Ramesh
1
10085
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which you may have seen many times in one form or another. An analysis of this example produces several problems that are not obvious but which will seriously limit your ability to use hierarchies like the example in a real program. Then, the article...
0
8297
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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...
0
8600
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
7311
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...
1
6162
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
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();...
1
2726
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
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
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.