🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.codewarrior
Article #1119

Re: throw-ing exceptions at interrupt time?

#1119
From: Ron Hunsinger
Date: Fri, 26 Oct 2001 17:32
106 lines
4814 bytes
In article <b53899e6.0110171315.5036e57c@posting.google.com>,
elgoog@strangelight.com (Jason Lamport) wrote:

> I noticed some interesting behaviour when playing with a little test
> program:
> ________________test.cpp__________
> class Error {
>         public:
>                 Error();
>                 virtual ~Error();
> };
> 
> Error::Error() {
>         printf( "Instantiating Error object %X\n", (long) this );
> }
> 
> Error::~Error() {
>         printf( "Destructing Error object %X\n", (long) this );
> }
> 
> void funcA();
> void funcB();
> 
> void funcA() {
>         printf( "Entering funcA()\n");
>         try {
>                 printf( "Trying funcB()\n");
>                 funcB();
>         } catch (Error& e2) {
>                 printf( "Rethrowing object %X\n", (long) &e2 );
>                 throw( e2 );
>         }
> }
> 
> void funcB() {
>         printf( "Throwing Error object....\n");
>         throw( Error() );
> }
> 
> int main() {
>         printf( "Entering main()\n");
>         try {
>                 printf( "Trying funcA()\n");
>                 funcA();
>         } catch (Error& e1) {
>                 printf( "Caught Error object %X\n", (long) &e1 );
>         }
>         printf( "leaving main()\n");
>         return 0;
> }
> ___________end____________
> 
[snip]
> 
> The output was also interesting:
> 
> _____________output of test.cpp____________
> Entering main()
> Trying funcA()
> Entering funcA()
> Trying funcB()
> Throwing Error object....
> Instantiating Error object 598CC58
> Rethrowing object 598CC58
> Destructing Error object 598CC58
> Caught Error object 598CC74
> Destructing Error object 598CC74
> leaving main()
> ________________end________________
> 
> It seems that re-throwing the object copies the object to another
> memory location (further up the stack?), destructs the old copy, but
> does *not* call the new copy's constructor. (Notice that the output
> indicates two destructor calls but only one constructor call.) This
> could cause some very hard-to-find bugs if one were relying on the
> usual assumption that constructor calls and destructor calls always
> occur in matched pairs.

The program is doing exactly what you told it to do:

a) You didn't rethrow the exception. Instead, you threw a copy of it.
To rethrow the same exception, say

    throw;              // rethrow e2

instead of

    throw (e2);         // throw copy of e2

b) The copy was constructed using the copy constructor, not the default
constructor. You supplied your own printf-calling default constructor,
but you neglected to supply a copy constructor, so the compiler
generated one for you (and didn't bother to put a printf in it). If you
want to see copies taking place, you need to also supply a copy
constructor, something like:

    Error::Error (const Error& rhs) {
        printf( "Copying Error object %X into %X\n",
                (long) &rhs, (long) this );
    }

Also, note that printf is not exception-safe. That doesn't matter in
this program, but I mention it in passing since this thread is about
what's safe to do from an exception handler.

-Ron Hunsinger
Technical Details
Message-ID: <261020011032564187%hunsinger@mac.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!news.gtei.net!newscon02.news.prodigy.com!newsmst01.news.prodigy.com!prodigy.com!postmaster.news.prodigy.com!newssvr14.news.prodigy.com.POSTED!not-for-mail
References: <b53899e6.0110152106.7d06e0fb@posting.google.com> <8CA031356232A730.29E614E5B9E85DCC.837A0ADCBC1AE7F6@lp.airnews.net> <b53899e6.0110171315.5036e57c@posting.google.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f635e,54ce753f0fa01891
X-Google-Attributes: gidf635e,public
X-Google-Thread: 10bcce,d3234953ce62cee2
X-Google-Attributes: gid10bcce,public
X-Google-ArrivalTime: 2001-10-26 10:33:03 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!news.gtei.net!newscon02.news.prodigy.com!newsmst01.news.prodigy.com!prodigy.com!postmaster.news.prodigy.com!newssvr14.news.prodigy.com.POSTED!not-for-mail
From: Ron Hunsinger <hunsinger@mac.com>
Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac
Subject: Re: throw-ing exceptions at interrupt time?
Message-ID: <261020011032564187%hunsinger@mac.com>
References: <b53899e6.0110152106.7d06e0fb@posting.google.com> <8CA031356232A730.29E614E5B9E85DCC.837A0ADCBC1AE7F6@lp.airnews.net> <b53899e6.0110171315.5036e57c@posting.google.com>
Organization: ErsteSoft
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-transfer-encoding: 8bit
User-Agent: YA-NewsWatcher/3.1.8
Lines: 106
NNTP-Posting-Host: 216.103.86.8
X-Complaints-To: abuse@prodigy.net
X-Trace: newssvr14.news.prodigy.com 1004117526 ST000 216.103.86.8 (Fri, 26 Oct 2001 13:32:06 EDT)
NNTP-Posting-Date: Fri, 26 Oct 2001 13:32:06 EDT
X-UserInfo1: FKPO@MC@@S@CBQD[@ZJ@NFTBTR\B@GXLN@GZ_GYO^ZWZUYICD^RAQBKZQTZTX\_I[^G_KGFNON[ZOE_AZNVO^\XGGNTCIRPIJH[@RQKBXLRZ@CD^HKANYVW@RLGEZEJN@\_WZJBNZYYKVIOR]T]MNMG_Z[YVWSCH_Q[GPC_A@CARQVXDSDA^M]@DRVUM@RBM
Date: Fri, 26 Oct 2001 17:32:06 GMT