January 25, 2021
Hot Topics:

Best Practices in Java Exception Handling

  • By Joydip Kanjilal
  • Send Email »
  • More Articles »

Exceptions are exceptional conditions that occur at runtime; these are errors that happen at the time when your program is in execution. If exceptions are not properly handled in your program, such errors terminate the flow of control at execution time. In this article, I will present some tips and tricks to handle exceptions efficiently in Java.

Exception handling is a non-functional requirement in any application. It is a cross-cutting concern that should be taken care of to avoid runtime errors that would otherwise creep in when the application is in execution. The Java Programming Language provides the try, catch, finally, throws and throws keywords to work with exceptions. Here is a list of some best practices that one should follow when programming in Java:

Acquire Resources Late and Release Them Early

This is a well-known saying: You should acquire the resources you need in your application as late as possible and release them as early as possible. Resources should be released in the finally block if exception blocks have been used in your code. Releasing resources in the finally block is preferred because the finally block is guaranteed to be executed irrespective of whether an exception has occurred or not. Here, resources imply a file handle, a database connection, and so forth. Another important point you should remember is that you should throw exceptions early in your code. You should catch your exceptions late; leave it to the caller to decide when to catch the exception that might be thrown. Exception messages should be as descriptive as possible and logged properly. I'd prefer to use an asynchronous logger always so that the main thread is not blocked.

Include Detailed Stack Trace

You should not shallow exceptions or re-throw exceptions without preserving the stack trace information. Preserving the stack trace when working with exceptions is of utmost importance to help you debug or troubleshoot your exception properly. If you are throwing a new exception, ensure that you pass the original exception object to the constructor of the newly created exception instance so that the stack trace information is preserved.

Here is an example:

try {
   /* some code that might raise an exception */
} catch( Exception e ) {
   // Note that the initial exception is lost.
   throw new Exception();

In the preceding code snippet, the exception stack trace information is lost because a new exception instance is created without preserving the original exception object. A better alternative to this is shown in the code snippet that follows:

try {
   /* some code that might raise an exception */
} catch( Exception e ) {
   // Note that the initial exception is lost.
   throw new CustomException("There was an error", e);

In the previous code snippet, you'll observe that the original exception instance is preserved and is passed to the constructor of the custom exception class.

Document Exception Blocks

You should avoid using empty catch blocks; these are meaningless and should be avoided. You also should document the exception blocks properly with meaningful statements that justify the usage of such exception blocks. This is more important if you are developing a API that you would like the developers to use. Also, it is advisable to use the Java standard exceptions instead of creating your own exception classes; the standard exceptions are widely in use and your developers would be aware of them more than the custom exception classes you create. This promotes maintenance and consistency in the application's source code.


Here's a useful link to further reading on this topic: http://www.javacodegeeks.com/2013/07/java-exception-handling-tutorial-with-examples-and-best-practices.html.


You should use exceptions only when they are needed because exceptions are costly, and using them in your code would be a hindrance as far as the application performance is concerned. As the name suggests, use exceptions only in exceptional situations—only when they are needed. In this article, we have had a look at some of the best practices when working with exceptions in Java. Happy reading!

This article was originally published on February 2, 2015

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date