try/catch idiom in std.datetime
Walter Bright
newshound2 at digitalmars.com
Mon Nov 18 12:18:31 PST 2013
On 11/18/2013 11:44 AM, Andrei Alexandrescu wrote:
> 1. Consider:
>
> this(in DateTime dateTime, immutable TimeZone tz = null) nothrow
> {
> try
> this(dateTime, FracSec.from!"hnsecs"(0), tz);
> catch(Exception e)
> assert(0, "FracSec's constructor threw when it shouldn't have.");
> }
>
> That's because FracSec.from!"hnsecs"(n) may throw for some values of n. To me,
> this is overkill. Clearly there must be a way to construct a zero time interval
> without much code being executed at all, let alone checks and exceptions and
> whatnot. (In particular FracSec.zero looks like the ticket.) Furthermore, having
> a simpler constructor call a more complicated constructor reminds one of the
> mathematician who throws away the water in the kettle to regress to an already
> solved problem.
I agree. But I'll add that it would be even better to redesign FracSec.from so
it never throws at all - have it assert(0) for invalid values being passed to it.
Exceptions should not be thrown for invalid arguments. Data validation should be
handled separately.
More information about the Digitalmars-d
mailing list