- Exception Handling Strategies
- Exception Handling Strategy - Overview
- Exception Handling Requirements
- Application Survival
- Notifying Relevant Parties
- Error Diagnostics and Reproduction
- Separation of Abstraction Layers
- More Readable and Maintainable Code
- Error Location and Context
- Error Causes, Types and Reactions
- Strategy Elements
- Error Detection
- Error Information Gathering
- Throwing Exceptions
- Propagating Exceptions
- Catching Exceptions
- An Exception Handling Strategy Template
- An Exception Class Template
- Throwing the AppException
- Propagating the AppException
- The ErrorInfo List
- Catching the AppException
- Avoid Exception Hierarchies
Exception Handling Strategy - Overview
An exception handling strategy consists of all actions and conventions necessary to properly handle exceptions in your application.
The following diagram shows an overview of the various parts of an exception handling strategy, from the error is detected, all the way up to where the error is handled.
|An exception handling strategy overview.|
At the bottom of the strategy is errror detection. You detect an error either youself, or you catch a third party exception (AKA alien exception).
At the second level you try local exception handling if possible. This is where you e.g. send your request to a backup server or wait X seconds and try again, etc. You do what you can, if anything, to handle the error and continue whatever operation your application was currently carrying out.
At the third level, when local error handling did not work, you gather all the information necessary to diagnose, reproduce and report the error.
At the fourth level you convert the caught exception, or the error detected, into an application specific exception, and throw it. You add any information necessary to properly handle the exception.
At the fifth level you propagate the application specific exception up the call stack, closing any opened resources on the way up (like files, network connections, freeing allocated buffers etc.). Additionally, you add any contextual information which may be useful in determining the cause and severity of the error.
At the sixth level you catch and react to the exception. At this point local exception handling is no longer possible, so most often the only thing your application can do is to notify users, and non-users of the exception. If the exception is really, really bad your application may even shut down gracefully.