Exception Handling

Learn about important topics in exception handling.

Perhaps exception handling is one of those topics that deserve a lot of attention but get none. Most of the exception handling techniques that I have seen in real-world applications catch everything and then either continue execution with a possibly corrupt state or throw an error to kill the program entirely even if it is not necessary. I know that such code blocks are often written based on the developer’s gut feeling rather than a clear understanding of the exception handling guidelines. Therefore, in this section, I hope to clarify a few critical matters around this topic.

Why catch exceptions?

A general rule is to catch only those exceptions from which you can recover. All other exceptions need to bubble up the call stack, hoping that an appropriate error handler will catch them. If you want to catch all exceptions regardless of your ability to recover from them, try to answer the following question: what would you do with an exception that indicates ...