In software, an exception is an event that occurs unexpectedly. An exception is exceptional behavior. In any modern object-oriented programming language, developers can define their on exceptions that inherit from the languages' base exception class. Defining your own exceptions and using them as flow control mechanisms in your code, if used cautiously, can yield very robust code.
The main use case for using this pattern is to simplify large branching flows of control. For example, say you have defined some method m1 that is invoked in another method you currently implementing, m2. The control flow in m2 is determined by the outcome of m1. There could potentially be several new branching flows in m2 each with subsequent levels of branching. This would eventually make m2 unmaintainable. An alternative is to implement m2 to only handle the basic success case. Any other cases are handled by custom exceptions. In this case, the invocation of m1 would have to handle everything that m1 can raise. Other than that, it will carry on as normal. m2 shouldn't care so much about m1s' failures.
The exceptions handle all the exceptional flows in you application. That is the basic idea. You have to use your imagination to build an exceptional vocabulary in the domain in which you are working. The exercise of building a vocabulary of what can go wrong in your system is a useful tool in its own right.
CodeSmith Tools recently released a new product, CodeSmith Insight, that is an excellent tool for Exception Driven Development. Insight's application integration allows you to (among other things) report all unhandled exceptions just by referencing the Insight client assembly. Also, possibly best of all, it's free to get started!
ReplyDeleteCheck it out at http://www.codesmithinsight.com/