473,666 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User-defined exceptions


Hello all,

Unfortunately, the reference I have is a bit slim on describing how to
create user-defined exceptions derived from std::exception. I think what I
have below will work, but is it the way the mechanism was intended to be
used?

Thanks,
Dave

class cyclic_t: public std::exception
{
public:
cyclic_t(const std::string &what_param) : what_str(what_p aram) {}
virtual const char *what() const throw() {return what_str.c_str( );}

private:
std::string what_str;
};

Jul 22 '05 #1
5 6765
Dave wrote:
...
Unfortunately, the reference I have is a bit slim on describing how to
create user-defined exceptions derived from std::exception. I think what I
have below will work, but is it the way the mechanism was intended to be
used?
...
class cyclic_t: public std::exception
{
public:
cyclic_t(const std::string &what_param) : what_str(what_p aram) {}
virtual const char *what() const throw() {return what_str.c_str( );}

private:
std::string what_str;
};
...


In many cases this is an acceptable approach. But keep in mind such
things as:

1. 'std::string' is a class that allocates dynamic memory, which means
that this particular approach to implementing custom exceptions is not
appropriate for implementing, for example, "out of dynamic memory"
exceptions. In more general words, it is certainly a good idea to
implement exception classes so that they don't try to throw exceptions
themselves. You exception class doesn't follow this recommendation.

2. The implicitly declared destructor for your exception class will have
"unlimited" exception specification (because of 'std::string's
destructor), while 'std::exception ' destructor's exception specification
is 'throw()'. In C++ the derived class' virtual method is not allowed to
have exception specification that is wider than corresponding base class
virtual method's exception specification. Your code is ill formed,
because it violates this requirement.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2

"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:vs******** ****@news.super news.com...
Dave wrote:
...
Unfortunately, the reference I have is a bit slim on describing how to
create user-defined exceptions derived from std::exception. I think what I have below will work, but is it the way the mechanism was intended to be
used?
...
class cyclic_t: public std::exception
{
public:
cyclic_t(const std::string &what_param) : what_str(what_p aram) {} virtual const char *what() const throw() {return what_str.c_str( );}
private:
std::string what_str;
};
...


In many cases this is an acceptable approach. But keep in mind such
things as:

1. 'std::string' is a class that allocates dynamic memory, which means
that this particular approach to implementing custom exceptions is not
appropriate for implementing, for example, "out of dynamic memory"
exceptions. In more general words, it is certainly a good idea to
implement exception classes so that they don't try to throw exceptions
themselves. You exception class doesn't follow this recommendation.

2. The implicitly declared destructor for your exception class will have
"unlimited" exception specification (because of 'std::string's
destructor), while 'std::exception ' destructor's exception specification
is 'throw()'. In C++ the derived class' virtual method is not allowed to
have exception specification that is wider than corresponding base class
virtual method's exception specification. Your code is ill formed,
because it violates this requirement.

--
Best regards,
Andrey Tarasevich


Wow, your second points brings up something really interesting! What you
are saying makes perfect sense. Yet when I look at the Standard, I see
standard exceptions that don't explicitly state that their destructor has an
exception specification of throw (). For example, consider 19.1.1. Is this
a problem with the Standard?????

Along the same lines, I see that the standard exception classes also use
std::string to specify what what will return(). These standard exception
classes also seem to be throwing caution to the wind with regard to your
first point too!

I hope we can get a good thread started on this. I'd love to hear what
people have to say and get this hammered out...

Thanks!
Dave
Jul 22 '05 #3
Dave wrote:
...
In many cases this is an acceptable approach. But keep in mind such
things as:

1. 'std::string' is a class that allocates dynamic memory, which means
that this particular approach to implementing custom exceptions is not
appropriate for implementing, for example, "out of dynamic memory"
exceptions. In more general words, it is certainly a good idea to
implement exception classes so that they don't try to throw exceptions
themselves. You exception class doesn't follow this recommendation.

2. The implicitly declared destructor for your exception class will have
"unlimited" exception specification (because of 'std::string's
destructor), while 'std::exception ' destructor's exception specification
is 'throw()'. In C++ the derived class' virtual method is not allowed to
have exception specification that is wider than corresponding base class
virtual method's exception specification. Your code is ill formed,
because it violates this requirement. ...
Wow, your second points brings up something really interesting! What you
are saying makes perfect sense. Yet when I look at the Standard, I see
standard exceptions that don't explicitly state that their destructor has an
exception specification of throw (). For example, consider 19.1.1. Is this
a problem with the Standard?????


No, there's no problem here. Firstly, take a look at 18.6.1. The
declaration of the destructor of 'std::exception ' _does_ explicitly
include an exception specification. And it is 'throw()', exactly as I
said above.

As for the other standard exception classes (such as 'std::logic_err or'
in 19.1.1), I still don't see any problem here. They all inherit from
'std::exception ' and nowhere in the standard it says that their
destructors have exception specification different from the one that
'std::exception ::~exception()' has - that would be simply illegal
because that would violate 15.4/3.

Note also, that when user defined class declares no destructor, the
implicitly declared one will have exception specification derived in
accordance with the rules described in 15.4/13. It's the combination of
15.4/13 and 15.4/3 is what makes your code ill-formed. But I never said
it is hopeless. You _can_ have a data member of 'std::string' type
inside your 'std::exception '-derived class, but first you have to take
one or more steps in order to make your code well-formed and relatively
safe.

Firstly, you have to provide an explicit declaration of your class'
destructor with explicit exception specification. For example

class cyclic_t : public std::exception
{
...
~cyclic_t() throw()
{}
...
}

Now the code is well-formed already and should compile. However, if
during the destruction of a 'cyclic_t' object the destructor of
'what_str' subobject suddenly thrown an exception, the control will go
into 'std::unexpecte d' and you know what happens next. If such behavior
is unacceptable in your case, you have to take another step - provide a
function-level try-block for 'cyclic_t's destructor which will intercept
such exceptions.

That's probably what standard exception classes would do if they wanted
to contain an 'std::string' object as an immediate subobject.
Along the same lines, I see that the standard exception classes also use
std::string to specify what what will return(). These standard exception
classes also seem to be throwing caution to the wind with regard to your
first point too!
I don't see any evidence of that either. These classes accept
'std::string's as their constuctor's parameters. That doesn't
necessarily mean that they _store_ 'std::string's as subobjects. And
even if they do, there's still a way to do it relatively safely, as I
explained above. Moreover, using 'std::string' as a member subobject
could be perfectly reasonable in such exception classes as
'std::logic_err or', 'std::range_err or' etc. But I would be extremely
surprised if 'std::bad_alloc ' contained a data member of 'std::string' type.
I hope we can get a good thread started on this. I'd love to hear what
people have to say and get this hammered out...


I think you can find quite a lot of information on this topic if you
google for it of just read the relevant articles on GotW site.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

"Andrey Tarasevich" <an************ **@hotmail.com> skrev i en meddelelse
news:vs******** ****@news.super news.com...
Dave wrote:
...
Unfortunately, the reference I have is a bit slim on describing how to
create user-defined exceptions derived from std::exception. I think what I have below will work, but is it the way the mechanism was intended to be
used?
...
class cyclic_t: public std::exception
{
public:
cyclic_t(const std::string &what_param) : what_str(what_p aram) {} virtual const char *what() const throw() {return what_str.c_str( );}
private:
std::string what_str;
};
...


[snip]
2. The implicitly declared destructor for your exception class will have
"unlimited" exception specification (because of 'std::string's
destructor), while 'std::exception ' destructor's exception specification
is 'throw()'. In C++ the derived class' virtual method is not allowed to
have exception specification that is wider than corresponding base class
virtual method's exception specification. Your code is ill formed,
because it violates this requirement.

--
Best regards,
Andrey Tarasevich


Hi Andrey

I believe you are a bit academic here. I can not imagine std::string's
destructor throwing. This would be very bad design and imply that lots of
normal stuff would be unable to work. As an example,
std::vector<std ::string> would be a no-no.

Actually, I do not believe that any well-designed class would EVER throw in
its destructor.... far too much code depends on this not ever happening.

Kind regards
Peter Koch Larsen
Jul 22 '05 #5
Peter Koch Larsen wrote:
"Andrey Tarasevich" <an************ **@hotmail.com> skrev i en meddelelse
news:vs******** ****@news.super news.com...
Dave wrote:
> ...
> Unfortunately, the reference I have is a bit slim on describing how to
> create user-defined exceptions derived from std::exception. I think what I > have below will work, but is it the way the mechanism was intended to be
> used?
> ...
> class cyclic_t: public std::exception
> {
> public:
> cyclic_t(const std::string &what_param) : what_str(what_p aram) {} > virtual const char *what() const throw() {return what_str.c_str( );} >
> private:
> std::string what_str;
> };
> ...


[snip]
2. The implicitly declared destructor for your exception class will have
"unlimited" exception specification (because of 'std::string's
destructor), while 'std::exception ' destructor's exception specification
is 'throw()'. In C++ the derived class' virtual method is not allowed to
have exception specification that is wider than corresponding base class
virtual method's exception specification. Your code is ill formed,
because it violates this requirement.

--
Best regards,
Andrey Tarasevich


Hi Andrey

I believe you are a bit academic here. I can not imagine std::string's
destructor throwing. This would be very bad design and imply that lots of
normal stuff would be unable to work. As an example,
std::vector<std ::string> would be a no-no.

Actually, I do not believe that any well-designed class would EVER throw in
its destructor.... far too much code depends on this not ever happening.
...


Well, I could be a bit academic in my first point, but not in the second
one. The bottom line is that this code is ill-formed. It does not
compile. Even if 'std::string's destructor does not actually throw
anything, it still has unlimited exception specification. This
immediately causes the implicitly declared destructor of class
'cyclic_t' to have unlimited exception specification (see 15.4/13),
which in turn will violate the requirements 15.4/3.

For some reason Comeau only issues a warning about this problem

"ComeauTest .c", line 4: warning: exception specification for implicitly
declared
virtual function "cyclic_t::~cyc lic_t" is incompatible with
that of
overridden function "std::exception ::~exception"
class cyclic_t: public std::exception
^

GCC 3.2 issues an error

ConsoleTest.cpp :5: looser throw specifier for `virtual
cyclic_t::~cycl ic_t()'
/usr/include/c++/3.2/exception:54: overriding `virtual
std::exception: :~exception() throw ()'

Was something relevant to this issue changed in TC1?

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6

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

Similar topics

60
7251
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
3
4133
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability. The User Experience, or how the user experiences the end product, is the key to acceptance. And that is where User Interface Design enters the design process. While product engineers focus on the technology, usability specialists focus on the user...
6
11276
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
1
7565
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
1
2130
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new to programming, but I am new to ASP.NET and Web application design in general... loved the concept of user controls and dynamically adding them to a page. So what I wound up with was an application that dynamically loads two user controls directly...
7
2912
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
0
3930
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something like this is a bug or that .NET doesn't support what I trying to do. I hope that one that is is microsoft certified read this because this must be a bug.
2
4817
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be required: <%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %> Assuming that the ASPX page doesn't use a code-behind, I can access the properties, events etc. of the user control in the ASPX page in this way (assume that the ASPX page...
1
1963
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users are created but the account is disabled (see the flag User.AccountDisabled = False ). There is also another problem even if the user does not exist , the application returns to me with the message that the user already exist. Thank you for...
0
3222
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have given all the attributes which are needed, for the user, in the code and also the proper path where the user has to be added. Please have a look at my code CODE] // This is a class file which stores all the info required for the user
0
8448
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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
8783
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
8552
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
7387
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
4198
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.