Monday 25 March 2013

When to use Exception Handling?

        Exception handling should be used judiciously. Earlier I wrote about fail fast vs fail safe and that article loosely relates to this context and my personal preference is to fail fast. Exception handling does not mean that we should take the program flow in an alternate direction than the intended one and which may cause issues in future. We should be cautious when we say handling, as in the name of handling, an issue should not be carried over to a different context of the program as it will come back to haunt us.
So when do we use exception handling, it is when we foresee that a problem may occur at run time and we know a possible solution to it at design time itself. Then at run time if that solution path is chosen it should not alter the core objective of the program. Cases like, just logging the problem and proceeding with the flow does not fit into exception handling. A popular mistake done by java developers is to use exception handling for flow control. Though we have if-else and other options, we tend to fall on exception handling side and use it as flow control mechanism which is poor.

No comments:

Post a Comment