Exceptions are for exceptional program flow – situations that are not ordinarily expected to occur during normal functioning of the program.

Following are some expected events that would normally warrant a return code instead of an exception.

Expected events
Incorrect password
Invalid value found while validating user input
Account balance too low

Of course, some of these events border on being expected or exceptional, but here are some examples of exceptional events.

Exceptional events
Database insert failed
Database unavailable
Logic problem while validating user input
File is locked
Disk full

It is better to use data structures to represent expected events.  For example,  a validation function might return a Messages structure that contains errors, warnings and information messages instead of throwing an exception.

blog comments powered by Disqus