|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2005-11-10 17:31:43
Alexander Nasonov <alnsn-boost_at_[hidden]> writes:
> How do you implement more complex finally block?
>
> FILE* files[10] = {};
>
> BOOST_FINALLY_BEGIN( (files) )
> {
> for(int i = 0; i < 10; ++i)
> if(files[i])
> ::fclose(files[i]);
> } BOOST_FINALLY_END
>
> for(int i = 0; i < 10; ++i)
> files[i] = ::fopen(get_nth_filename(i), "r");
Boost::lambda has for loops, neh?
> I prefer not hiding catch(...) from a user.
Practically speaking, destructors are much better than catch(...) for
finally-like operations on many platforms. Several of them have a
similar problem to Windows, where catch(...) catches asynchronous
exceptions.
> BTW, I can modify the code to allow throw-spec before open brace
>
> BOOST_FINALLY_BEGIN( (files) ) throw()
> {
> for(int i = 0; i < 10; ++i)
> if(files[i])
> ::fclose(files[i]);
> } BOOST_FINALLY_END
>
> Also, there is also function-try-block in C++:
>
> BOOST_FINALLY_BEGIN( (files) ) try
> {
> for(int i = 0; i < 10; ++i)
> if(files[i])
> ::fclose(files[i]);
> } catch(...) {} BOOST_FINALLY_END
I don't know what your plans for using a function-try-block are, but
AFAICT that construct is only useful for exception translation during
data member initialization.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk