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 

Is throwing an exception in a handler legal?

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





PostPosted: Thu Feb 23, 2006 1:06 am    Post subject: Is throwing an exception in a handler legal? Reply with quote



Hi all,
Until now I was pretty sure that throwing an exception from inside a
catch() handler other than re-throwing the current handled exception
was a no-no.

But now I cannot find any reference in the Standard or even on TCPPL
supporting thi claim.

Is it true? If so, what are the references?

Thanks
Gianluca Silvestri


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





PostPosted: Thu Feb 23, 2006 11:06 am    Post subject: Re: Is throwing an exception in a handler legal? Reply with quote



Gianluca Silvestri wrote:
Quote:
Hi all,
Until now I was pretty sure that throwing an exception from inside a
catch() handler other than re-throwing the current handled exception
was a no-no.

But now I cannot find any reference in the Standard or even on TCPPL
supporting thi claim.

Is it true? If so, what are the references?

It's not true. You can throw anything you want from inside a catch
handler.

Bob


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





PostPosted: Thu Feb 23, 2006 1:06 pm    Post subject: Re: Is throwing an exception in a handler legal? Reply with quote



Gianluca Silvestri <dishsi (AT) tin (DOT) it> wrote:
Quote:
Until now I was pretty sure that throwing an exception from inside a
catch() handler other than re-throwing the current handled exception
was a no-no.

mistake. Throwing an exception inside catch is rather normal practice if
exception must be partly handled locally and then passed to caller:

try
{
//...
}
catch(...)
{
log_error();
throw;
}


..... in translation of exceptions:

try
{
//...
}
catch(std::exception& e)
{
throw ApplicationSpecificError(typeid(e), e.what());
}


.... and in exception filtering:

int filter()
{
try
{
throw;
}
catch (std::bad_alloc& e)
{
return E_MEMORY_DEPLETED;
}
catch (std::overflow_error& e)
{
return E_OVERFLOW;
}
catch (ApplicationSpecificError& e)
{
return e.code();
}
catch (std::exception& e)
{
return E_GENERAL_FAILURE;
}
}

// binary module boundary, must not throw C++ exceptions
int foo() throw ()
{
try
{
//...
}
catch(...)
{
return filter();
}
return SUCCESS;
}


B.


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





PostPosted: Thu Feb 23, 2006 3:06 pm    Post subject: Re: Is throwing an exception in a handler legal? Reply with quote

In article <1140617420.274026.83680 (AT) o13g2000cwo (DOT) googlegroups.com>,
Gianluca Silvestri <dishsi (AT) tin (DOT) it> writes
Quote:
Hi all,
Until now I was pretty sure that throwing an exception from inside a
catch() handler other than re-throwing the current handled exception
was a no-no.

But now I cannot find any reference in the Standard or even on TCPPL
supporting thi claim.

Is it true? If so, what are the references?

Difficult to provide references when it isn't (true that is):-)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


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





PostPosted: Thu Feb 23, 2006 3:06 pm    Post subject: Re: Is throwing an exception in a handler legal? Reply with quote

Gianluca Silvestri wrote:
Quote:
Hi all,
Until now I was pretty sure that throwing an exception from inside a
catch() handler other than re-throwing the current handled exception
was a no-no.

But now I cannot find any reference in the Standard or even on TCPPL
supporting thi claim.

Is it true? If so, what are the references?

Might you be thinking of the guideline that exceptions should not be
thrown from destructors?

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





PostPosted: Thu Feb 23, 2006 7:06 pm    Post subject: Re: Is throwing an exception in a handler legal? Reply with quote

Gianluca Silvestri wrote:
Quote:
Hi all,
Until now I was pretty sure that throwing an exception from inside a
catch() handler other than re-throwing the current handled exception
was a no-no.

But now I cannot find any reference in the Standard or even on TCPPL
supporting thi claim.

Is it true? If so, what are the references?


Thanks to all. I don't remember when or where I read about this.
Probably I was confused by the rules about throwing an exception in a
destructor during stack unwinding.

Gianluca Silvestri


[ 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.