The Right Approach to Exceptions

Robert Jacques sandford at jhu.edu
Sat Feb 18 17:01:31 PST 2012


On Sat, 18 Feb 2012 18:40:50 -0600, H. S. Teoh <hsteoh at quickfur.ath.cx> wrote:
> 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.

That an argument of an internationalization module as part of the standard library, not for or against a particular exception module. I don't know what a good/robust module would look like, but many open source projects simply use a global string[string] and it seems to work okay for them.


More information about the Digitalmars-d mailing list