It Will Never Work in Theory: Live!

Our next set of online lightning talks is happening April 25-26, 2023. Check out the speakers and get your ticket now!

Java Exception Handling

Reviewed by Greg Wilson / 2016-04-26
Keywords: Programming Languages

Nakshatri2016 Suman Nakshatri, Maithri Hegde, and Sahithi Thandra: "Analysis of exception handling patterns in Java projects". Proceedings of the 13th International Conference on Mining Software Repositories, 10.1145/2901739.2903499.

Exception handling is a powerful tool provided by many programming languages to help developers deal with unforeseen conditions. Java is one of the few programming languages to enforce an additional compilation check on certain subclasses of the Exception class through checked exceptions. As part of this study, empirical data was extracted from software projects developed in Java. The intent is to explore how developers respond to checked exceptions and identify common patterns used by them to deal with exceptions, checked or otherwise. Bloch's book "Effective Java" was used as reference for best practices in exception handling—these recommendations were compared against results from the empirical data. Results of this study indicate that most programmers ignore checked exceptions and leave them unnoticed. Additionally, it is observed that classes higher in the exception class hierarchy are more frequently used as compared to specific exception subclasses.

This paper's abstract underplays the bleakness of its findings. Rather than being used to make software more robust, exceptions are either ignored or used as a debugging aid. For example, the most common catch block is one that logs the error rather than trying to recover from it; the next most common are to do nothing at all (20% of cases), or to convert the checked exception into an unchecked exception so that it can be ignored. Similarly, most programmers ignore Java's carefully thought out exception hierarchy and simply catch Exception (78%) or Throwable (84%) rather than any of their subclasses.

The only conclusion I can draw from this is that exception-based error handling has failed to achieve what its creators intended. That's slightly different from being a complete failure: based on Nakshatri et al's findings, it appears that exception are useful as a debugging aid during development. And it's possible that the real failure is educational: despite the ubiquity of distributed systems, most undergraduate programs still spend approximately zero time on error handling and recovery. Either way, I think language developers have their work cut out for them.

Note: I would have liked to compare these findings with those in Kery, Le Goues, and Myers' "Examining Programmer Practices for Locally Handling Exceptions", which was published in the same conference. It also explores Java exception handling, but I was unable to find an open access copy anywhere on the web. It's unfortunate that its authors have chosen not to share their work with practitioners who might be interested in their findings.