Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-03-20 17:58:48


Jesse Jones wrote:
> >void clipboard::put_text(std::string const& text) {
> > HGLOBAL handle = ::GlobalAlloc( GHND, text.length() + 1 );
> > // copy text to allocated memory
> > // ..
> > // copy text to clipboard
> > bool result1 = ::OpenClipboard(0);
> > assert(result1);
> > bool result2 = ::EmptyClipboard();
> > assert(result2);
> > bool result3 = ::SetClipboardData(CF_TEXT, handle);
> > assert(result3);
> > bool result4 = ::CloseClipboard();
> > assert(result4);
> >}
>
> This is the problem with VERIFY: when it's there you're
> tempted to use it. :-)

Same with 'assert' :)

[snip]

> In the code snippet above the first three error conditions should be
> handled with exceptions (or the moral equivalent).

I disagree. Given the calling sequence, it is guaranteed that all the calls
will succeed if both the OS environment and your program are in consistent
state (no leaked/invalid handles, no corrupted memory regions etc.). If they
are not, it's a problem of a code that put your program/OS into such state,
but for sure not the 'clipboard::put_text' function; assertions in that
function only help to detect such problems early and track their sources
more easily, but generally you better to find the real bugs instead of
trying to live with their consequences. In any case, throwing the exception
here doesn't give you anything, except that it forces all clients of the
function to wrap the 'put_text' call into try-catch block that will be empty
anyway.

Aleksey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk