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 33,183 of 33,346    |
|    DeMarcus to All    |
|    Re: RAII advice (for a RAII newbie)    |
|    31 Aug 13 12:50:10    |
   
   From: demarcus_at_hotmail_com@tellus.orb.dotsrc.org   
      
   > // GOOD (RAII)   
   > {   
   > CleanupType cleanup;   
   > dosomething();   
   > }   
   >   
   > // ????   
   > {   
   > try   
   > {   
   > dosomething();   
   > cleanup();   
   > }   
   > catch (const std::exception& ex)   
   > {   
   > cleanup();   
   > }   
   > catch (...)   
   > {   
   > cleanup();   
   > }   
   > }   
   >   
   > Why is the 2nd (RAII) approach superior to the third approach? I   
   > presume there's more to it than "appearance"? While the third   
   > approach looks more complicated, it doesn't have to worry about a   
   > "CleanupType"...   
   >   
      
   I see at least two problems with the third example.   
      
   [*] You should be very careful with catching ellipsis (...). If you do   
   you must at least rethrow, like this:   
      
   catch (...)   
   {   
    cleanup();   
    throw;   
   }   
      
   (I challenge anyone reading this to come up with examples where one   
   would ever need to catch ellipsis)   
      
   [*] You get into problems where you want to return early (opposite of   
   Single-Exit-Point). Here's an example that would be cumbersome with   
   your approach.   
      
   int randomNumber()   
   {   
    try   
    {   
    dosomething();   
      
    lockWeatherVariable();   
    switch( theWeather )   
    {   
    case SUNNY:   
    cleanup();   
    unlockWeatherVariable();   
    return 35;   
    case CLOUDY:   
    cleanup();   
    unlockWeatherVariable();   
    return 93;   
    case RAINY:   
    cleanup();   
    unlockWeatherVariable();   
    return 62;   
    default:   
    break;   
    }   
    }   
    catch( ... )   
    {   
    cleanup();   
    unlockWeatherVariable();   
    // Do you see the error that leaked in here?   
    throw;   
    }   
      
    cleanup();   
    unlockWeatherVariable();   
    return 4711;   
   }   
      
   Compare above with the following:   
      
   int randomNumber()   
   {   
    ScopedLock lock;   
    CleanupType cleanup;   
      
    dosomething();   
      
    switch( theWeather )   
    {   
    case SUNNY:   
    return 35;   
    case CLOUDY:   
    return 93;   
    case RAINY:   
    return 62;   
    default:   
    break;   
    }   
      
    return 4711;   
   }   
      
   RAII is a beautiful way of relieving your mind from what need to be   
   cleaned up, where and when.   
      
   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