Boost logo

Boost :

Subject: Re: [boost] [exception] ostringstream like
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-08-16 14:47:01


On Mon, Aug 16, 2010 at 7:21 AM, Fabien Castan
<fcastan_at_[hidden]> wrote:
> Create an error sentence with some values inside is not really an advanced
> string manipulation.

The problem is that you're composing a message for the user at the
time you're reporting the error. This is incorrect because:

1) The code that detects and reports the problem may not have enough
information available to compose the error message;

2) The message may have to be localized. Imagine having to support 20
different languages with different grammars, in all places where you
throw exceptions!

Consider the following error handling strategy:

1) At the point of the throw, put only the information you have (for
example, error code, etc.) in an exception and throw;

2) In contexts higher up the stack, catch(boost::exception&), add more
relevant information (such as a file name, etc.) and rethrow;

3) Let the catch site deal with formatting a user-friendly message
from the information stored in the exception.

4) In a top-level context, catch(...) and log the string returned by
boost::current_exception_diagnostic_information() which isn't
user-friendly but it is generated automatically and contains
information that can hopefully tell you what went wrong so you can add
the missing catch.

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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