The Right Approach to Exceptions

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Feb 23 07:47:55 PST 2012


On Thu, Feb 23, 2012 at 02:57:43AM -0800, Jonathan M Davis wrote:
[...]
> DbC tends to work better with internal stuff where you control both
> the caller and the callee, whereas defensive programming works better
> with public APIs.  But regardless, which is best to use depends on the
> situtation and what you're goals are.
[...]

The way I understand it, DbC is used for ensuring *program* correctness
(ensure that program logic does not get itself into a bad state);
defensive programming is for sanitizing *user input* (ensure that no
matter what the user does, the program doesn't get into a bad state).

That's why DbC is compiled out in release mode -- the assumption is that
you have thoroughly tested your program logic and verified there are no
logic problems. Input sanitizing is never compiled out, because you
never know what users will do, so you always have to check.

The two do somewhat overlap, of course. For example, failing to sanitize
user input may eventually lead to passing invalid arguments to an
internal function.


T

-- 
Only boring people get bored. -- JM


More information about the Digitalmars-d mailing list