Clean Code and The Art of Exception Handling

Grace Angelica
MeetU Engineering
Published in
2 min readMay 2, 2018

Exception by the ancient were used to alter the flow of the program and avoid hardware failures. Today, exception is define as conditions requiring special processing and often changing the normal flow of program execution. So, exceptions require special treatment, and an unhandled exception may cause unexpected behavior. Exceptions are a fundamental element of modern programming; they exist to make our software better. Rather than fearing exceptions, we should embrace them and learn how to benefit from them.

How to handle exception:

These are some recommendation practices to embrace exceptions

  1. Maintainability, can easily find and fix new bugs, without the fear of breaking current functionality.
  2. Extensibility, provides flexibility, and enables a high level of reusability for our code base.
  3. Readability, can easily read the code and discover it’s purpose without spending too much time digging.

But most of languages have its own exception class, organized in an inheritance hierarchy, like any other OOP class. To maintan the readability, maintainability, and extensibility of our code, we should create our own subtree of the application.

The last but not least, when handling exception it is important to do it immediately. An exception can be handled at any point in the call stack, and any point in the class hierarchy, so knowing exactly where to handle it can be mystifying. But it is ok though if you want to analize it first, sometimes the result when we think about this through, we will find more appropiate place to handle the exception.

Here are the 7 most important ones that help you get started or improve your exception handling in java:

1. Clean up Resources in a Finally Block or Use a Try-With-Resource Statement

You should, therefore, put all your clean up code into the finally block or use a try-with-resource statement.

2. Prefer Specific Exceptions

Always try to find the class that fits best to your exceptional event, e.g. throw a NumberFormatException instead of an IllegalArgumentException. And avoid throwing an unspecific Exception.

3. Document the Exceptions You Specify

Make sure to add a @throws declaration to your Javadoc and to describe the situations that can cause the exception.

4. Throw Exceptions With Descriptive Messages

You should explain the reason for the exception in 1–2 short sentences. That helps your operations team to understand the severity of the problem, and it also makes it easier for you to analyze any service incidents.

5. Catch the most specific exception first

6. Don’t catch Throwable

If you use Throwable in a catch clause, it will not only catch all exceptions; it will also catch all errors. Errors are thrown by the JVM to indicate serious problems that are not intended to be handled by an application. Typical examples for that are the OutOfMemoryError or the StackOverflowError. Both are caused by situations that are outside of the control of the application and can’t be handled.

7. Don’t ignore exceptions

“This will never happen” problem might be change in the future. Someone might remove the validation that prevented the exceptional event without recognizing that this creates a problem.

--

--