
Alexander Nasonov <alnsn-boost@yandex.ru> 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