The Right Approach to Exceptions

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Feb 18 16:40:50 PST 2012


On Sat, Feb 18, 2012 at 06:09:30PM -0600, Andrei Alexandrescu wrote:
> On 2/18/12 6:03 PM, Jonathan M Davis wrote:
[...]
> >If we have GetOptException, it should have a variable for which flag failed.
> >Exception doesn't have that. And what if the flag failed because it was given a
> >bad argument? Then the exception needs a field for that argument. Then you can
> >get something like
> >
> >try
> >     getopt(args, ...)
> >catch(MissingArgumentException mae)
> >{
> >     stderr.writefln("%s is missing an argument", mae.flag);
> >     return -1;
> >}
> >catch(InvalidArgumentException iae)
> >{
> >     stderr.writelfln("%s is not a valid argument for %s. You must give it a
> >%s.", mae.arg, mae.flag, mae.expectedType);
> >     return -1;
> >}
> >catch(UnknownFlagException ufe)
> >{
> >     stderr.writefln("%s is not a known flag.", ufe.ufe);
> >     return -1;
> >}
> >catch(GetOptException goe)
> >{
> >     stderr.writefln("There was an error with %s",  goe.flag);
> >     return -1;
> >}
> >//A delegate that you passed to getopt threw an exception.
> >catch(YourException ye)
> >{
> >     //...
> >}
> >catch(Exception e)
> >{
> >     stderr.writeln("An unexpected error occured.");
> >     return -1;
> >}
> >
> >You can't do _anything_ like that right now.
> 
> Of course I can. They call it toString(). The code above pretty much
> proves my point with so much wonderful irony.
[...]

One word: internationalization. Then toString() falls flat on its face.

You can't even fix this by having Phobos do the translation internally.
You can't expect users of Phobos to only ever support languages that
Phobos has been previously translated to, for one thing. That would be a
crippling disability.


T

-- 
Valentine's Day: an occasion for florists to reach into the wallets of
nominal lovers in dire need of being reminded to profess their
hypothetical love for their long-forgotten.


More information about the Digitalmars-d mailing list