Program logic bugs vs input/environmental errors (checked exceptions)

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 6 06:45:58 PDT 2014


On 10/5/14, 11:31 PM, Jacob Carlborg wrote:
> On 05/10/14 18:18, Andrei Alexandrescu wrote:
>
>> Exceptions are all about centralized error handling. How, and how often,
>> would you handle FileNotFoundException differently than
>> PermissionDeniedException?
>
> Probably not that often. But I don't think it's up to "File" to decide
> that. I think "File" should throw a specific error as possible
> containing all details it has.

Sure, but that can be distinguished by payload, not type.

> Then it's up to the user of "File" how to
> handle the exception. If you're not interested in the differences
> between FileNotFoundException and PermissionDeniedException, then catch
> the base class instead.
>
> The details provided between these two exception could be different as
> well. FileNotFoundException should contain the path of the file that
> wasn't found. PermissionDeniedException should contain some information
> about if it was the source or target that caused the exception to be
> thrown. Think of a copy or move operation.
>
> How would one localize error messages if the specific exception is not
> known?

Knowledge doesn't have to be by type; just place data inside the 
exception. About the only place where multiple "catch" statements are 
used to make fine distinctions between exception types is in sample code 
showing how to use multiple "catch" statements :o). This whole notion 
that different exceptions need different types is as far as I can tell a 
red herring.

Andrei



More information about the Digitalmars-d mailing list