Boost logo

Boost :

From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2020-06-02 01:15:28


On Mon, Jun 1, 2020 at 4:36 PM Gavin Lambert via Boost <
boost_at_[hidden]> wrote:
> With exceptions we now have nested_exception, but they both might wish
> to transport a filename, for example, and these may be different at each
> level.

Two options:

1) You want to capture the error objects reported by a lower level
function, and then later look at them elsewhere. Here is an example on how
to do this:

With exceptions:
https://github.com/zajo/leaf/blob/master/examples/capture_in_exception.cpp?ts=4
.
With leaf::result<T>:
https://github.com/zajo/leaf/blob/master/examples/capture_in_result.cpp?ts=4
.

This is analogous to nested exceptions, the exact context type is erased
and the context is transported in an exception or in a leaf::result<T>.

2) A lower level function has failed but you don't want to report this as a
new error so to speak, you just want to add more info to the existing
failure. Ideally the two functions would use different error types, and in
this case it'll just work, just use preload(). Otherwise, it might make
sense to wrap the individual types that clash, rather than the whole
context. Here is an example:

try_catch(
  [&] { ...something that could throw... },
  []( leaf::error_info const & e, leaf::e_file_name const & fn )
  {
    e.error().load( e_wrapped_file_name { fn } );
    throw;
  } );

This handler will execute for any exception that has e_file_name. We just
wrap it in another type and rethrow.

(The reason why this is needed is that a context can hold no more than one
object for each type. In principle this can be changed, in which case you
could have two e_file_name stored in the same context, associated with two
different error_ids.)


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