Asserts, Exceptions (and Validation)

This blog post was originally created at 2008-09-06

At work we have had a bit of a discussion about Debug.Assert and Exceptions. With a little help from google and Steve McConnell's great book Code Complete we have come to the following definitions:

Both Assertions and Exceptions has to do with technical stuff. Business stuff should be handled by validation logic. Business logic should never appear in assertions and exceptions.

Assertions

Is an assumption, it should never happen that this assertion is broken.

If the assumption is broken, a bug fix should be done.

Error handling should not be done on assertions by default, however in robust code you should do errorhandling on assertions.

Exceptions

First of all Exceptions are for exceptionel behaviour!

Exceptions are for unpredictable but expected behaviour.

You can not know when you get a null pointer because the file does not exist, but you must expect that this can happen.

Exceptions should always be logged.

comments powered by Disqus