Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-03-20 19:22:23


It seems to me that assertion is inappropriate here and exceptions should be
used instead, though. Am I missing something important?

----- Original Message -----
From: "Aleksey Gurtovoy" <alexy_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, March 20, 2001 4:30 PM
Subject: RE: [boost] Re: Runtime error reporting

> David Abrahams wrote:
> > Actually, I disagree. IMO, the stuff in assert() is very easy
> > to separate from the mainline code. Everyone must follow the rule
> > "no side-effects in assert" anyway. Adding a VERIFY() macro just
> > gives us another macro to keep track of. How does it help in any
> > substantive way?
>
> Taking Win32 API as an example, in theory almost every function call there
> can fail, and consistent checking for it really helps to detect induced
> errors (and their sources, of course). The problem with having only
classic
> 'assert'-like macro as a checking tool for such errors is that consistent
> application of "check a return value of every API function call" policy
> makes your code much more bloated and clumsy, e.g.:
>
> 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);
> }
>
> vs.
>
> void clipboard::put_text(std::string const& text) {
> HGLOBAL handle = ::GlobalAlloc( GHND, text.length() + 1 );
> // copy text to allocated memory
> // ..
> // copy text to clipboard
> sys_verify(::OpenClipboard(0));
> sys_verify(::EmptyClipboard());
> sys_verify(::SetClipboardData(CF_TEXT, handle));
> sys_verify(::CloseClipboard());
> }
>
> You can beautify the first version a little by rearranging the code like
> this:
>
> { bool result = ::OpenClipboard(0); assert(result); }
> { bool result = ::EmptyClipboard(); assert(result); }
> // etc.
>
> but IMO 'sys_verify' version is still much more appealing here.
>
> Aleksey
>
>
> List-Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


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