try/catch idiom in std.datetime
Jacob Carlborg
doob at me.com
Tue Nov 19 23:43:06 PST 2013
On 2013-11-20 08:22, Jouko Koski wrote:
> Ok. What if the "user" is writing a library code which calls other
> system utilities? If the validation fails, is the answer now assert?
>
> I don't have a definite answer. However, I tend to follow the practice
> that all public API should do validation and throw on failures. I use
> assert for catching my own mistakes in more or less internal stuff.
I neither have a definite answer. It depends on what that particular
function is doing.
> Usually I don't find separate validation API functions very practical.
> One can forget or ignore to do validation, just like one can fail to
> check errno after a call. Of course, validation is necessary and
> exceptions can assert (sic!) this. It might be more of a personal
> preference, but often I catch exceptions only for error reporting
> purposes. Exception is an indication of an unexpected condition in the
> program. The solution is to fix the program to be prepared for the
> condition, not trying to fix the condition afterwards as the exception
> has already fired. Not all APIs support this approach.
I all depends on what API's and what the program is doing. Trying to
read a file which you don't have permission to read, or doesn't exist
would probably throw an exception. But that is not an error by the
programmer. It could be perfectly fine to catch that exception and ask
the user of the program to try a different file.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list