Java Exception handling best practices

Handling Java exceptions can be tricky.

Which Java exceptions to catch, which exceptions to rethrow, and which exceptions to ignore completely is a difficult skill to develop.

But there are a number of Java Exception handling best practices that provide software developers guidance when they develop new applications.

Top 10 Java Exception handling best practices

Here are the top 20 Java exception handling best practices software developers should follow:

  1. Be careful what you log
  2. Don’t bury thrown exceptions
  3. Use a global Exception handler
  4. Don’t close resources manually
  5. Throw early and handle exceptions late
  6. Don’t log and rethrow Java exceptions
  7. Check for suppressed exceptions
  8. Explicitly define exception in the throws clause
  9. Catch the most explicit exception first
  10. Use modern exception handling semantics

Be careful what you log

Software developers must always be mindful of the security and privacy rights of their clients.

Data logged by a Java application may be moved to various low cost storage systems and viewed by any number of developers and administrators attempting to solve a bug.

If any protected data is written the the log files, your company will be out of compliance, and you might be out of a job.

Don’t bury thrown exceptions

Don’t catch and exception and then do nothing with it. That’s known as burying an exception and it is definitely not a Java exception handling best practice.

At the very least log the name of the exception and the message associated with it. That way information about an exceptional situation can be extracted from the logs.

Buried exceptions makes troubleshooting Java applications extremely hard.

Use a global Exception handler

There will always be instances of the uncaught RuntimeException that will creep into your code.

Always include a global Exception handler to deal with any uncaught exceptions. This way you can not only log and potentially handle the Exception, but it will also stop your app from crashing when a runtime exception is thrown.

Don’t close resources manually

Another important Java exception handling best practice is to allow the JVM to call the close() method of closeable resources. Don’t do close resources yourself.

This is easily accomplished by initializing resources within a try-with-resources block. The following example shows the try-with-resources semantics in action:

public class TryWithResourcesExample {
   public static void main(String[] args) throws Exception {
 
     try (Door door = new Door()) {
       door.swing();
     } catch (Exception e) { /* do something */ }
     } finally { /* do something */ }
     /* The door will automatically be closed after the code block */
   }
 }

The JVM will close the resource for you when the try…catch block completes, eliminating the potential for messy and difficult to troubleshoot resource leaks.

Throw early and handle exceptions late

As soon as an exception condition happens in your code, throw an Exception. Don’t wait for any additional lines of code to run before terminating execution of the method you are in.

In terms of catching exceptions, that function should go towards the end of a method. This way you have fewer catch blocks in your methods, making your code much easier to read and maintain.

Don’t log and rethrow

When an Exception occurs you should either:

  • Log the Exception and carry on with your application
  • Rethrow the Exception and let another method log the details

You should never do both. You should never log and then rethrow the same exception as they do in the following example:

/* log and rethrow exception example */
try {
  Class.forName("com.mcnz.Example");
} catch (ClassNotFoundException ex) {
  log.warning("Class was not found.");
  throw ex;
}

Doing so causes code duplication, and it also litters the log files with duplicate entries, making it much more difficult to troubleshoot code.

Check for suppressed exceptions

The suppressed exception is a relatively new language feature that not all developers are aware of.

Basically, with the introduction of the try-with-resources function, it became possible for two exceptions to be thrown at the same time. This situation can easily be checked simply by querying the for the suppressed exception’s existence, as is done in the example below:

try ( Door door = new Door() ) {
  door.swing(); /* Throws the SwingException */
}
catch (Exception e) {
  System.out.println("Primary Exception:  " + e.getClass());
  if (e.getSuppressed().length > 0) {
    System.out.print("Suppressed Exception: " + e.getSuppressed()[0]);
  }
}

The only way to know if this scenario has occurred is to check if the target Exception also contains a suppressed exception as well.

Explicitly define exceptions in the throws clause

Lazy developer will use the generic Exception class in the throws clause of a method. Doing so is not a Java exception handling best practice

Instead, always explicitly state the exact set of exception a given method might throw. This allows other developers to know the various error handling routines they can employ if a given method fails to execute properly.

Catch the most specific exception first

This is more of a compiler requirement than a Java exception handling best practice, but a developer should always catch the most specific exception first, and the least specific one last.

If this rule is not followed, the JVM will generate a compile time error with a fairly cryptic error message that is difficult to understand.

Make you software development life easier by always catching the most specific exception in your code first.

Use modern exception handling techniques

Is Java verbose? It is if you stick with the 20 year old exception handling techniques.

Java has added many features in the field of error and exception handling that simplify development and greatly reduce the verbosity of Java code.

Take advantage of the ability to catch multiple exceptions in a single catch block, automatically close resource with the try-with-resources block, and use RuntimeExceptions to avoid forcing other developers to handle the exceptions you throw.

Follow these 10 Java exception handling best practices, and you’ll find yourself learning to love dealing with exceptions in Java.

 

 

 

 

 

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close