Step 28: Don’t Nail Your Program into the Upright Position ~Verity Stob
This is the 28th Step towards gaining the Programming Enlightenment series. If you didn’t learn the 27th Step, read it.
What is Exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
When to handle Exception?
Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following:
- A
try
statement that catches the exception. Thetry
must provide a handler for the exception, as described in Catching and Handling Exceptions. - A method that specifies that it can throw the exception. The method must provide a
throws
clause that lists the exception, as described inSpecifying the Exceptions Thrown by a Method.
Code that fails to honor the Catch or Specify Requirement will not compile.
Is handling Every Exception great?
Obviously we don’t want our application to catch at any point of time. So, we start adding Exception handling mechanism, which later turns into a library.
We don’t have any crashes now. But, the downside is that now magically the problems in our application seem to disappear. We no longer know the bugs dwelling down inside the rabbit hole of the Exception handling mechanism.
UI designers’ rule: NEVER LET THE USER SEE AN EXCEPTION REPORT, rather as though this settled the matter, what with it being in caps and everything. But, as a developer the least we can do it spit out logs for whatever exception occurred and try to remedy it.
TL;DR Handle Exceptions, but don’t use try-catch everywhere. If used, use it wisely.
Go to 27th Step
Go to the 29th Step.
References:
- 97 things Every Programmer Should Know ~ Git Book
- 97 Things Every Programmer Should Know ~ Paperback
- Exception Handling ~ Wiki