Boost logo

Boost :

From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2006-07-04 10:51:01


On 7/4/06, Emil Dotchevski <emildotchevski_at_[hidden]> wrote:
>
> I don't think it can be completely avoided. If you throw failed<foo>, you
> should catch(foo &) to handle the exception. At this point you do need a
> dynamic_cast to get to the exception_info sub-object. You could have a
> virtual function in foo to get you an exception_info *, but I don't think
> that this is appropriate.
>

Let's see:

template <typename _T>
class failed : public exception_info, public std::exception {
  public:
    failed() { };
    explicit failed (const _T & e) : _wrapped_exception(e) { };

    // ... other common methods ...
};

//...
try {
  ...
} catch (exception_info & e) {
  ...
}

where `e` is already an exception_info reference.

> > This also seems like too much work for exception handling -- which
> > seems harder to do than just creating my own exception class which
> > derives from std::exception, and then a custom constructor that
> > contains a copy of the information object that I know I want to be
> > available from those that will be receiving the exception.
>
> When you say that you can just create your own exception class, do you mean
> that you'd create a different exception class for each combination of
> context information you want to bundle in it?
>

Granted that the idea is that the exceptions themselves will be
designed by me, and that I know what code will require what
information, it sounds like a reasonable way of going about it --
compared to doing a dynamic_cast all the time, and the interface being
not so clear and "developer friendly".

> > Is there a good reason why you're not using a "composite-style" [see
> > GoF composite pattern] exception if you intend to be able to add
> > information as the exception is propagated through the try {}
> > catch(...) {} blocks?
>
> If you catch(...), you can't do anything to the exception object. That's why
> you need class exception_info: so you can catch(exception_info&) and add
> info to it, regardless of T in a throw failed<T>(). Or did you mean to use
> composite pattern to organize the information in an exception_info? Could
> you illustrate this with an example?
>

I meant for general catch blocks, I didn't intend to say that the
literal catch(...) {} blocks. Something like this:

struct composite_exception {
  std::vector<composite_exception> _exceptions;
  void add(const composite_exception & e) {
    _exceptions.add(composite_exception(e));
  }

  explicit composite_exception(const composite_exception & other) :
    _exceptions(other._exceptions) { };

  composite_exception() {};

  // add common methods here, like "what()"
};

So you can do something like:

try {
} catch (composite_exception & e) {
  //... iterate through the vector
};

-- 
Dean Michael C. Berris
C/C++ Software Architect
Orange and Bronze Software Labs
http://3w-agility.blogspot.com/
http://cplusplus-soup.blogspot.com/
Mobile: +639287291459
Email: dean [at] orangeandbronze [dot] com

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