Exception Handling Strategies
- 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
Error Detection
Jakob Jenkov |
Error detection is what your code does to detect an error, preferrably before the error happens. For instance, you validate input parameters in a method, before using them. Here is a simple example:
public void doSomething(int value, Employee targetEmployee){ if(value < 20) { throw new IllegalArgumentException("Value too low"); } if(targetEmployee == null){ throw new IllegalArgumentException("Target employee was null"); } ... do actual work. }
Notice the if-statements in the beginning of the method. These are error detecting statements. They check the input parameters for invalid values, and throw an exception if they are invalid.
The exception throwing itself in the example above is not part of the error detection. That is part of the "Throwing the exception", which I get into later. I have just shown them in the examples for clarity.
Tweet | |
Jakob Jenkov |