Step 21: Distinguish Business Exceptions from Technical ~ Dan Bergh Johnsson
“I call it my billion-dollar mistake. It was the invention of the null reference in 1965.”
~ Tony Hoare
Equally Good API isn’t built right the first time. Even more, it’s difficult to change it later. Don’t even put into the picture the pain to support for the API changes.
What is an Exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions
What are the types of Exception?
We can classify Exception into two types.
- Technical Exception
These can be termed as events which go wrong because of technical errors. If not handled properly they propagate to application crashes.
ERROR: NullPointerException encountered while trying to adding String to Deque that is set to null.
Null Pointer Exception
- Business Exception
These are the events which were put in place because of business rules. They prevent the users from using the application.
Logon failure: unknown user name or bad password
// This may be because of user name unknown or wrong password
Insufficient Funds: cannot make transaction
// This may be because user doesn't have enough fund in their account
Technical and Business Exceptions should be handled separately.
It will create a potential source of confusion if we represent them within the same hierarchy.
We should make it clear, what conditions it is required to ensure before calling, and what situations it is supposed to handle.
Separating these two will give clarity and increases chances that technical exceptions are handled subtly by application framework (exception handling libraries), while business domain exceptions are handled by the client code.
TL;DR Understand the exceptions in your application. Do not mix Business Exception with Technical ones.