Home Exceptions

Fail early

Have your exception checks take place as soon as possible by moving them to the top of your methods. Think of exception checks as gatekeepers that prevent corrupt data from entering your method. You want your gatekeepers to be stationed close to the gate, not in the middle of your castle.

Exception checks and if statements

Exception checks should always be placed into individual if statements with no attached else statements.

Custom error messages

Although this is not strictly speaking required, you should always include a custom error message for every exception you throw.

Commenting exceptions

We will elaborate more on this subject on the sections about commenting, but as a note, you must document exactly what kind of exceptions you will be throwing, and under exactly what conditions.