Replace Exception with Test
You throw an exception in a place where a simple test would do the job?
Replace the exception with a condition test.
Exceptions should be used to handle irregular behavior related to an unexpected error. They should not serve as a replacement for testing. If an exception can be avoided by simply verifying a condition before running, then do so. Exceptions should be reserved for real errors.
For instance, you entered a minefield and triggered a mine there, resulting in an exception; the exception was successfully handled and you were lifted through the air to safety beyond the mine field. But you could have avoided this all by simply reading the warning sign in front of the minefield to begin with.
- A simple conditional can sometimes be more obvious than exception handling code.
How to Refactor
Create a conditional for an edge case and move it before the try/catch block.
Move code from the
catchsection inside this conditional.
catchsection, place the code for throwing a usual unnamed exception and run all the tests.
If no exceptions were thrown during the tests, get rid of the