"Jacob Carlborg" wrote:
On 2013-11-19 18:11, Jouko Koski wrote:
If the "user level" validation fails, shall we throw or assert or...?
Throw.
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.
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.
--
Jouko