C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

throw specification of a function.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Min-Koo Seo
Guest





PostPosted: Sun Dec 26, 2004 10:27 am    Post subject: throw specification of a function. Reply with quote



Hello.

I've seen the following code from C++ Primer Plus 4th edition.

class problem {...};
....
void super() throw (problem &)
{
...
if (oh_no)
{
problem oops(); // construct object
throw oops; // throw it
...
}
....
try {
super();
}
catch(problem & p)
{
....
}

While reviewing the code, I've found some strange part in the specification
of super(). It says, "throw (problem &)." But, as you can see, super
throw an instance of oops which is in stack.

So, I think it would be totally illegal to specifiy a function as
'throw &some_object.' Is my opinion is right?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
L.Suresh
Guest





PostPosted: Sun Dec 26, 2004 6:56 pm    Post subject: Re: throw specification of a function. Reply with quote



15.1/3
"The type of the throw-expression shall not be an incomplete type, or a
pointer or reference to an incomplete type, other than void*, const
void*,
volatile void*, or const volatile void*"

So, you can throw by reference. But the way that is done in your
example is not right. The temporary is destroyed when control transfers
from throw to the exception handler invalidating the refernce. OTOH,
you can throw a valid reference.

--lsu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Barbati
Guest





PostPosted: Sun Dec 26, 2004 6:57 pm    Post subject: Re: throw specification of a function. Reply with quote



Min-Koo Seo wrote:
Quote:
Hello.

I've seen the following code from C++ Primer Plus 4th edition.

class problem {...};
...
void super() throw (problem &)
{
...
if (oh_no)
{
problem oops(); // construct object
^^^^^^

Very wrong!! This line does not construct a "problem" object, but it
declares a function named oops with no arguments returning a "problem".

The correct line should be:

problem oops; // construct object
Quote:
throw oops; // throw it
...
}
...
try {
super();
}
catch(problem & p)
{
...
}

While reviewing the code, I've found some strange part in the specification
of super(). It says, "throw (problem &)." But, as you can see, super
throw an instance of oops which is in stack.

According to §15.4/7: "A function is said to allow an exception of type
E if its exception-specification contains a type T for which a handler
of type T would be a match (15.3) for an exception of type E." In this
specific case, a handler of type "problem&" would be a good match for a
thrown object of type "problem", so *apart from the mistake above* the
code is valid.

Quote:
So, I think it would be totally illegal to specifiy a function as
'throw &some_object.' Is my opinion is right?


This sentence makes no sense... What do you mean by "specify a function
as 'throw &some_object'"? You can't put objects or expressions in
exception specifications...

But maybe you meant this kind of code:

if (oh_no)
{
problem oops; // construct object
throw &oops; // throw it
}

syntactically speaking, this code is legal, but it throws an "object*"
not an "object&". Unfortunately, this code will probably make the
program crash, because you are throwing a pointer to a stack-based
object that is going to be destroyed during stack unwinding. The pointer
will be invalid long before it is even caught by a catch handler
incurring in undefined behaviour.

If you want to throw pointers to object, always allocate them on the
heap, as in:

if (oh_no)
{
problem* oops = new problem; // construct object
throw oops; // throw it
}

and remember to call delete in the catch handler!

HTH,

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Jan 05, 2005 10:24 am    Post subject: Re: throw specification of a function. Reply with quote

L.Suresh wrote:
Quote:
15.1/3
"The type of the throw-expression shall not be an incomplete type, or a
pointer or reference to an incomplete type, other than void*, const
void*,
volatile void*, or const volatile void*"

The question was about exception-specifications, not throw-
expressions. However there is similar language describing the types
allowed in exception-specifications. I think it's misleading because
expressions never really have reference type (see 5/6) and neither do
exceptions (see 15.1/1). I don't see that including a reference type
in an exception-specification is meaningful.

Quote:
So, you can throw by reference. The temporary is destroyed when
control transfers from throw to the exception handler invalidating
the refernce. OTOH, you can throw a valid reference.

This is all wrong. Exceptions are always objects, not references. If
a throw-expression names a reference to an object that will be
destroyed before the exception is caught, the exception object must be
a copy of that object which remains valid as long as necessary
(15.1/3-4).

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
L.Suresh
Guest





PostPosted: Wed Jan 05, 2005 9:14 pm    Post subject: Re: throw specification of a function. Reply with quote

Thanks for pointing it out. I misunderstood 15.4/1 thinking that
references could be thrown.

Still it seems that you can say,

void f() throw(some_class&);

Which does not have any utility and is misleading, atleast to me Smile
--lsu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.