enforce()?

Leandro Lucarella luca at llucax.com.ar
Wed Jun 16 10:02:08 PDT 2010


Andrei Alexandrescu, el 16 de junio a las 07:53 me escribiste:
> >because it make the programmer to
> >not think about errors at all, just add and enforce() and there you go.
> >But when you need to be fault tolerant, is very important to know what's
> >the nature of the error, but thanks to enforce(), almost every error is
> >a plain Exception, no hierarchy, no extra info, all you can do to get
> >a little more info about what happened is to parse the exception string,
> >and that's not really an option.
> 
> I think there is no real need for exception hierarchies. I
> occasionally dream of eliminating all of the useless exceptions
> defined left and right in Phobos.

Exception hierarchy is only one way to discriminate error types. Extra
info, is another (like an error code). I agree that a *large* exception
hierarchy hurts more than it helps.

> >>And in fact, I think there's an errnoEnforce which throws a standard
> >>exception with the string error from the system.
> >
> >That's the only useful case of enforce, because it includes the
> >*important* information (the actual errno).
> >
> >There is also enforceEx!(), to use a custom exception, which practically
> >nobody uses (I counted only 4 uses in phobos).
> 
> I'd be hard pressed to find good examples of exception hierarchy
> use. Everybody talks about them but I've seen none.

I think Python has a good one. I find myself discriminating between
ValueError, IndexError, KeyError, OSError and IOError all the time.

> The fact that the coder doesn't need to think hard to use enforce()
> effectively is a plus, not a minus. An overdesigned enforce that
> adds extra burden to its user would have been a mistake.

That is, if you don't care on handling errors and let the program crash
with a backtrace, or add a big try {} catch (Exception) in the main. If
that's not the case, it only produce a false feeling that D (standard
library) is good handling errors when it's not, it's just a binary
"there is an error" - "there is no errors".

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
The average person laughs 13 times a day


More information about the Digitalmars-d mailing list