|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-18 07:28:54
From: "Kevlin Henney" <kevlin_at_[hidden]>
> > From: "Peter Dimov" <pdimov_at_[hidden]>
> >
> >From: "Kevlin Henney" <kevlin_at_[hidden]>
> >> I think this is an excellent suggestion. What are Booster preferences
> >> from the following possibilities:
> >>
> >> (1) Add specific throws:
> >> (a) Not at all.
> >> (b) Distinguished by initialiser string.
> >> (c) Distinguished by enum.
> >> (d) Both (b) and (c).
> >
> >I strongly suggest
> >
> >(e) distinguished by exception type.
>
> I should have mentioned why I didn't include this option: historically
> the original numeric_cast had multiple exception types, but there was a
> feeling that this was superfluous. I was assuming that the same would
> also apply to lexical_cast, for consistency. However, I think that now
> that you have suggested it, it ought to be up for consideration.
Perhaps I need to elaborate. :-)
Distinguishing different exceptions by type, rather than by enum, allows
clients to pick their own subset of exceptions that they are interested
with. If this weren't important, lexical_cast would simply throw
std::bad_cast.
In a real world application, I'll use both the initializer string (it's in
English, usually not suitable for the end user) for debug, log, auto bug
report purposes _and_ the exception type to present an appropriate error
description to the end user.
Depending on the application (and the user) I may decide to only catch
std::bad_cast and display a generic error message, or I may catch the
individual errors and display a message that is somehow tied to the error.
I hope this makes sense. :-)
It's true that individual exception types make the library code rather
verbose but I haven't found a better error handling solution.
For example, my I/O library defines:
class exception: public std::exception;
class logic_error: public exception;
class file_error: public exception;
class open_error: public file_error;
class read_error: public file_error;
class eof_error: public read_error;
class write_error: public file_error;
class seek_error: public file_error;
class compression_error: public exception;
class decompression_error: public exception;
class invalid_format: public exception;
class invalid_signature: public invalid_format;
class invalid_version: public invalid_format;
class invalid_count: public invalid_format;
In 'toy' programs (command line utilities) I never catch anything other than
io::exception.
-- Peter Dimov Multi Media Ltd. PS: string specializations? wstring specializations?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk