Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-11-12 11:07:55


On Tuesday 12 November 2002 08:43 am, Yitzhak Sapir wrote:
> I would like to do a loop on all elements of a vector, and for each one
> call some function. I'd like to attempt this function each time, even if
> exceptions are thrown, and since I don't care about those exceptions I'd
> like to wrap this in try{} catch(...) {} which I will refer to as an
> "exception sink". The important thing is that this action be tried for
> each element of the vector.
>
> For any particular function, I can write such a forwarding functor.
> However, I have to do this several times, and it appears to me that this
> is a rather general use rather than for any specific functor. However, to
> do this, I essentially have to replicate bind, only adding "try {}
> catch(...) {}" around it, calling it bind_with_exception_sink or
> something.
>
> A half-way solution would have been to perform the "placeholder binding"
> in one pass, and then pass the generated bound 0 argument functor to the
> exception-sink wrapper. Then the exception-sink wrapper only has to deal
> with one kind of functor. But this leaves the possibility of an exception
> happening during the placeholder binding.
>
> Anyhow, I'm at a loss to see any solution to this, but I thought this
> might have been nice if it could have been done. So for now, I end up
> writing a specific wrapper for each case. But I thought I'd raise this,
> in case any of you have ideas as to how it could be done, or even if it is
> worth doing in the first place. (A similar bind-like functor I can think
> of is one that calls new and then performs some operation, such as
> transform it to an auto_ptr).

Boost.Lambda can do this, because it has support for creating try/catch blocks
on-the-fly. See:
  http://www.boost.org/libs/lambda/doc/ar01s05.html#sect:exceptions

You will probably have a function object like this (untested):

  try_catch(
    bind(your_function_object, _1),
    catch_all(var(exceptions_caught)++)
  )

        Doug


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