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,358 of 33,346    |
|    DeMarcus to All    |
|    Will we ever be able to throw from a des    |
|    04 Jun 12 05:46:10    |
   
   From: use_my_alias_here@hotmail.com   
      
   Hi,   
      
   There is always a big discussion about exceptions and when and where they can   
   be thrown.   
      
   In Exceptional C++ by Herb Sutter, p.55, he states why throwing from   
   destructors is a bad thing. Apparently for a lot of people in the community   
   (including myself) it has been difficult to be convinced that the bad thing   
   with throwing from a destructor    
   is more than just an unsolved technical detail.   
      
   The other day I came up with an argument why we may never be able to throw   
   from a destructor. My "proof" is as follows.   
      
   1. Our prerequisite is that we either allow throwing from destructors, or we   
   do not. This is to conform to the programming model of least surprise.   
      
   2. We also assume that not every method will be able to provide the strong   
   exception-safety guarantee (by Abrahams).   
      
   3. Above two points would lead to the fact that a non-trivial (read throwing)   
   destructor will suffer from a higher probability of failing if an exception is   
   currently thrown from any of the class's methods.   
      
   4. All points above would prevent allocation of objects in the same scope as   
   they are used (see examples below).   
      
   Example A:   
      
   try   
   {   
    MyClass a;   
      
    a.doSomething();   
   }   
   catch( std::exception& e )   
   {   
    // If it's likely that the destructor will have problems   
    // after a thrown exception from doSomething(), then we will   
    // have double exceptions here. Even if we could chain them,   
    // we would have a hard time to handle the root cause.   
   }   
      
      
   Example B:   
      
   try   
   {   
    MyClass b;   
      
    try   
    {   
    b.print();   
    }   
    catch( std::exception& e )   
    {   
    // Amend the problem and rethrow.   
    throw;   
    }   
   }   
   catch( std::exception& e )   
   {   
    // If we assume that the inner try/catch amends all problems,   
    // the destructor could be allowed to throw but as we see we   
    // now lose the possibility to have ctor/dtor in the same scope   
    // as the method use. Therefore, the further we want to be able   
    // to throw the methods' exceptions, the further we must move   
    // ctor/dtor scope which renders exceptions useless in most   
    // cases.   
   }   
      
      
   Please give your comments and say whether you think my "proof" holds or not.   
      
   Regards,   
   Daniel   
      
      
   --   
    [ 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