Program logic bugs vs input/environmental errors

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 27 16:55:53 PDT 2014


On Sat, Sep 27, 2014 at 04:42:18PM -0700, Walter Bright via Digitalmars-d wrote:
> On 9/27/2014 4:33 PM, bearophile wrote:
> >Walter Bright:
> >
> >>As for the programmer wanting to know where the message "missing }"
> >>came from,
> >>
> >>    grep -r dmd/src/*.c "missing }"
> >>
> >>works nicely. I do that sort of thing all the time. It really isn't
> >>a problem.
> >
> >grep is not useful for the purposes explained in issue 13543 because
> >the file name is often inside a string variable, that is initialized
> >elsewhere or generated in some way. So the exception is useful to
> >know where's the instruction in user code that has tried the failed
> >I/O action, as I've explained in that issue.
> 
> Even if that is what you wanted, you won't get that from
> FileException, as it will only show file/lines emanating from calls
> inside std.file, not from higher level callers.
> 
> Besides, take a bit of care when formulating a string for exceptions,
> and you won't have any trouble grepping for it. This isn't rocket
> science.
> 
> Presenting internal debugging data to users for input/environmental
> errors is just bad programming practice. We shouldn't be enshrining it
> in Phobos and presenting it as a professional way to code.

My take on this, is that uncaught exceptions are a program bug. Any
messages displayed to the user ought to come from a catch block that not
only prints the exception message (*without* things like line numbers
and stack traces, btw), but also provides context (e.g., "Error in
configuration file section 'abc': illegal field value" instead of just
"illegal field value" with no context of where it might have been
triggered).

Uncaught exceptions (which ideally should only be Errors, not
Exceptions) are a program bug that ought to be fixed. In the case that
somehow one managed to elude your catch blocks, the full debug infodump
(source file, line number, stack trace) is useful for users to hand back
to you in a bug report, so that you can track down the problem. The user
should not be expected to understand the infodump from an uncaught
exception, whereas a message printed from a catch block ought to be
user-understandable (like "can't open 'myphoto.jpg': file not found",
not "internal error on line 12345" which makes no sense to a user).


T

-- 
Laissez-faire is a French term commonly interpreted by Conservatives to mean 'lazy fairy,' which is the belief that if governments are lazy enough, the Good Fairy will come down from heaven and do all their work for them.


More information about the Digitalmars-d mailing list