The Right Approach to Exceptions

Jonathan M Davis jmdavisProg at gmx.com
Tue Feb 21 11:47:58 PST 2012


On Tuesday, February 21, 2012 08:15:29 Andrei Alexandrescu wrote:
> On 2/21/12 4:40 AM, Vincent wrote:
> > On Saturday, 18 February 2012 at 18:52:05 UTC, Andrei Alexandrescu wrote:
> >> From experience I humbly submit that catching by type is most of the
> >> time useless.
> > 
> > Completely disagree. Types allow to control place for "catch". Say, some
> > deeply nested function catches its own exceptions, while outer function
> > catches the rest - exceptions higher in hierarchy. But to have benefit
> > you have to create exceptions hierarchy - this is the main point.
> 
> As the next hundreds of messages discuss, matters are not all that
> simple :o).

It probably should be though. I think that there's a lot of overthinking going 
on here.

We need to have our exceptions organized into a hierarchy so that you can 
catch them based on what went wrong. That is the main thing that we're missing 
IMHO.

Adding extra capabilities along side that is fine as long as they make sense. 
Adding a Variant[string] property for the purpose of putting non-standard 
information in your exceptions which don't merit a new exception type makes 
some sense. But if it makes sense to catch an exception based on that 
information, then it probably merits a new type. Adding improved string 
formatting cabilities also makes some sense.

But I don't think that we need to drastically overhaul how exceptions work, 
and a lot of this discussion is straying into left field.

- Jonathan M Davis


More information about the Digitalmars-d mailing list