home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,366 of 33,346   
   Dave Abrahams to francis.glassborow-AT-btinternet.co   
   Re: Will we ever be able to throw from a   
   05 Jun 12 10:30:35   
   
   From: dave@boostpro.com   
      
   on Mon Jun 04 2012, Francis Glassborow   
    wrote:   
      
   > From my perspective it is more than that. Once the body of a dtor   
   > has been entered the object's lifetime has ended. Now what happens   
   > when you throw from a dtor? The object is dead but has not been   
   > properly buried.   
      
   Personally, I find that kind of metaphor unhelpful.  What has burial   
   got to do with anything?   
      
   > How is the clean up supposed to happen?   
      
   It all depends what "the clean up" is, and whether there truly is any   
   left to be done for the object whose destructor threw.  The lifetime   
   of subobjects hasn't ended, and they will be destroyed as usual.  So   
   if the last thing I do in my destructor body throws an exception, what   
   does that mean?  The answer depends on how that operation was   
   specified and why I'm calling it.  It might be benign with respect to   
   program state (say, if I was trying to close a temporary file).   
      
   If something throws elsewhere in my destructor body, it means,   
   locally, what it means for any other function: the rest of that   
   destructor's body will be skipped unless there's an enclosing try   
   block.  This, too, is a manageable/understandable scenario.   
      
   > I once thought throwing from a dtor was fine but as I came to think   
   > about it I came to realise that if something happens within a dtor   
   > that means that it is not possible to complete the clean-up then the   
   > process needs something much more drastic than throwing an   
   > exception. Exceptions are designed to pass problems from the place   
   > where they can be detected to the place where they can be dealt   
   > with, but how can that apply to a dtor, either you can handle the   
   > problem locally or the problem cannot be handled but can only be   
   > ignored.   
      
   So in your view, there is a substantial difference between:   
      
      struct C { C(){throw 1;} };   
      void f()   
      {   
          C x;   
          ...   
      }   
      
   and   
      
      struct D { ~D(){throw 1;} };   
      void f()   
      {   
          {D x;}   
          ...   
      }   
      
   in terms of their meaningfulness or handle-ability?  I'd be interested   
   in your explanation, if so.  It seems to me that as the user of f(), I   
   don't care whether the exception came from C's constructor or D's   
   destructor.   
      
   --   
   Dave Abrahams   
   BoostPro Computing   
   http://www.boostpro.com   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca