The document outlines best practices for exception handling in Java, emphasizing the use of checked exceptions for recoverable errors and unchecked exceptions for programming errors. Key recommendations include avoiding the overuse of catch blocks, not swallowing exceptions, logging meaningful information, and ensuring exceptions are relevant to the context in which they occur. It also advises against using exceptions for flow control and encourages proper resource management with features like try-with-resources introduced in Java 7.
Introduction to best practices in exception handling. Presented by Lemİ Orhan ERGİN.
Importance of using checked exceptions for recoverable errors and unchecked for programming errors. Avoid overusing checked exceptions to maintain code readability.
Exceptions can slow down applications. Avoid swallowing exceptions as it results in loss of error context.
Declare specific exceptions in methods. Avoid catching generic exceptions and ensure proper wrapping to retain stack trace.
Logging or throwing exceptions but not both. Never throw exceptions from a finally block or catch them unless handled.
Avoid printStackTrace and use finally blocks for cleanup. Throw exceptions early and catch them late for better handling.
Exceptions should be descriptive to indicate their causes and not use numeric codes for error conditions.
Never use exceptions for flow control. Ensure clarity by having one try block per operation and handling exceptions outside loops.
Provide informative exception messages, document exceptions in code, and catch exceptions at the application's UI layer.
Citations of various resources and practices related to exception handling in Java.
Contact information of Lemİ Orhan ERGİN, the founder and author at agilistanbul.com.
1 Use Checked Exceptionfor Recoverable error & Unchecked Exception for programming error Checked exceptions ensures that you provide exception handling code for error conditions, which is a way from language to enforcing you for writing robust code, but same time it also add lots of clutter into code and makes it unreadable. Also, it seems reasonable to catch exception and do something if you have alternatives or recovery strategies.
3.
2 Avoid overusing CheckedException catch block with multiple exceptions catch (IOException|SQLException ex) { logger.log(ex); } automatic resource management with try-with-resources java7 java7 static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } } Checked Exception has there advantage in terms of enforcement, but at same time it also litters the code and makes it unreadable by obscuring business logic. You can minimize this by not overusing checked Exception which result in much cleaner code. You can also use newer Java 7 features like “one catch block for multiple exceptions” and “automatic resource management”, to remove some duplication.
4.
3 Converting Checked Exception intoRuntimeException try { riskyOperation(); } catch (IOException ioe) { throw new CustomRuntimeException(ioe); } This is one of the technique used to limit use of checked Exception in many of frameworks like Spring ,where most of checked Exception, which stem from JDBC is wrapped into DataAccessException, an unchecked Exception. This Java best practice provides benefits, in terms of restricting specific exception into specific modules, like SQLException into DAO layer and throwing meaningful RuntimeException to client layer.
5.
4 Remember Exceptions arecostly in terms of performance One thing which is worth remembering is that Exceptions are costly, and can slow down your code. Suppose you have method which is reading from ResultSet and often throws SQLException than move to next element, will be much slower than normal code which doesn't throw that Exception. So minimizing catching unnecessary Exception and moving on, without fixing there root cause. Don’t just throw and catch exceptions, if you can use boolean variable to indicate result of operation, which may result in cleaner and performance solution. Avoid unnecessary Exception handling by fixing root cause.
6.
5 Never swallow theexception in catch block catch (NoSuchMethodException e) { return null; } Doing this not only return “null” instead of handling or re-throwing the exception, it totally swallows the exception, losing the cause of error forever. And when you don’t know the reason of failure, how you would prevent it in future? Never do this !!
7.
6 Declare the specificchecked exceptions that your method can throw public void foo() throws Exception { } public void foo() throws SpecificException1, SpecificException2 { } Always avoid doing this as in above code sample. It simply defeats the whole purpose of having checked exception. Declare the specific checked exceptions that your method can throw. If there are just too many such checked exceptions, you should probably wrap them in your own exception and add information to in exception message. You can also consider code refactoring also if possible.
8.
7 Do not catchthe Exception class rather catch specific sub classes try { someMethod(); } catch (Exception e) { LOGGER.error("method has failed", e); } The problem with catching Exception is that if the method you are calling later adds a new checked exception to its method signature, the developer’s intent is that you should handle the specific new exception. If your code just catches Exception (or Throwable), you’ll never know about the change and the fact that your code is now wrong and might break at any point of time in runtime.
9.
8 Never catch Throwableclass try { someMethod(); } catch (Throwable t) { // handle throwable } Well, its one step more serious trouble. Because java errors are also subclasses of the Throwable. Errors are irreversible conditions that can not be handled by JVM itself. And for some JVM implementations, JVM might not actually even invoke your catch clause on an Error
10.
9 Always correctly wrapthe exceptions in custom exceptions so that stack trace is not lost catch (NoSuchMethodException e) { throw new MyServiceException("Some information: " + e.getMessage()); } catch (NoSuchMethodException e) { throw new MyServiceException("Some information: " , e); } Incorrect way of wrapping exceptions destroys the stack trace of the original exception, and is always wrong.
11.
10 Either log theexception or throw it but never do the both catch (NoSuchMethodException e) { LOGGER.error("Some information", e); throw e; } Logging and throwing will result in multiple log messages in log files, for a single problem in the code, and makes life hell for the engineer who is trying to dig through the logs.
12.
11 Never throw anyexception from finally block try { // Throws exceptionOne someMethod(); } finally { // If finally also threw any exception, // the exceptionOne will be lost forever cleanUp(); } This is fine, as long as cleanUp() can never throw any exception. In the above example, if someMethod() throws an exception, and in the finally block also, cleanUp() throws an exception, that second exception will come out of method and the original first exception (correct reason) will be lost forever. If the code that you call in a finally block can possibly throw an exception, make sure that you either handle it, or log it. Never let it come out of the finally block.
13.
12 Always catch onlythose exceptions that you can actually handle catch (NoSuchMethodException e) { throw e; } Well this is most important concept. Don’t catch any exception just for the sake of catching it. Catch any exception only if you want to handle it or, you want to provide additional contextual information in that exception. If you can’t handle it in catch block, then best advice is just don’t catch it only to re-throw it.
14.
13 Don’t use printStackTrace()statement or similar methods catch (NoSuchMethodException e) { System.out.println(e.getStackTrace()); } Never leave printStackTrace() after finishing your code. Chances are one of your fellow colleague will get one of those stack traces eventually, and have exactly zero knowledge as to what to do with it because it will not have any contextual information appended to it.
15.
14 Use finally blocksinstead of catch blocks if you are not going to handle exception try { someMethod(); } finally { cleanUp(); //do cleanup here } This is also a good practice. If inside your method you are accessing someMethod, and someMethod throws some exception which you do not want to handle, but still want some cleanup in case exception occur, then do this cleanup in finally block. Do not use catch block.
16.
15 Remember “Throw earlycatch late” principle This is probably the most famous principle about Exception handling. It basically says that you should throw an exception as soon as you can, and catch it late as much as possible. You should wait until you have all the information to handle it properly. ! This principle implicitly says that you will be more likely to throw it in the low-level methods, where you will be checking if single values are null or not appropriate. And you will be making the exception climb the stack trace for quite several levels until you reach a sufficient level of abstraction to be able to handle the problem.
17.
16 Always clean upafter handling the exception If you are using resources like database connections or network connections, make sure you clean them up. If the API you are invoking uses only unchecked exceptions, you should still clean up resources after use, with try – finally blocks. Inside try block access the resource and inside finally close the resource. Even if any exception occur in accessing the resource, then also resource will be closed gracefully. ! You can use new features Java7 to run auto-cleanup via try-withresources statement.
18.
17 Exception names mustbe clear and meaningful Name your checked exceptions stating the cause of the exception. You can have your own exception hierarchy by extending current Exception class. But for specific errors, throw an exception like “AccountLockedException” instead of “AccountException” to be more specific.
19.
18 Throw exceptions forerror conditions while implementing a method public void someMethod() { // on error 1 return -‐1; // on error 2 return -‐2; } If you return -1, -2, -3 etc. values instead of FileNotFoundException, that method can not be understand. Use exceptions on errors.
20.
19 Throw only relevantexception from a method Relevancy is important to keep application clean. A method which tries to read a file; if throws NullPointerException, then it will not give any relevant information to user. Instead it will be better if such exception is wrapped inside custom exception e.g. NoSuchFileFoundException then it will be more useful for users of that method.
21.
20 Never use exceptionsfor flow control Never do that. It makes code hard to read, hard to understand and makes it ugly.
22.
21 Never use exceptionsfor flow control in your program try { // do some logic throw new OperationException(); } catch (OperationException e) { // log the exception message // or throw a new exception } It is useless to catch the exception you throw in the try block. Do not manage business logic with exceptions. Use conditional statements instead.
23.
22 One try blockmust exist for one basic operation Granularity is very important. One try block must exist for one basic operation. So don't put hundreds of lines in a try-catch statement.
24.
23 Do not handleexceptions inside loops for (Message message:messageList) { try { // do something that can throw ex. } catch (SomeException e) { // handle exception } } try { for (Message message:messageList) { // do something that can throw ex. } } catch (SomeException e) { // handle exception } Exception handling inside a loop is not recommended for most cases. Surround the loop with exception block instead.
25.
24 Always include allinformation about an exception in single log message try { someMethod(); } catch (OperationException e) { LOGGER.debug(“some message”); // handle exception LOGGER.debug(“some another message”); } Using a multi-line log message with multiple calls to LOGGER.debug() may look fine in your test case, but when it shows up in the log file of an app server with 400 threads running in parallel, all dumping information to the same log file, your two log messages may end up spaced out 1000 lines apart in the log file, even though they occur on subsequent lines in your code.
26.
25 Pass all relevantinformation to exceptions to make them informative as much as possible catch (SomeException e) { logger.log(“error occurred”, e); } This is also very important to make exception messages and stack traces useful and informative. What is the use of a log, if you are not able to determine anything out of it. These type of logs just exist in your code for decoration purpose.
27.
26 Always terminate thethread which it is interrupted while (true) { try { Thread.sleep(100000); } catch (InterruptedException e) {} doSomethingCool(); } while (true) { try { Thread.sleep(100000); } catch (InterruptedException e) { break; } } doSomethingCool(); Some common use cases for a thread getting interrupted are the active transaction timing out, or a thread pool getting shut down. Instead of ignoring the InterruptedException, your code should do its best to finish up what it’s doing, and finish the current thread of execution.
28.
27 Use template methodsfor repeated try-catch class DBUtil{ public static void closeConnection(Connection conn){ try{ conn.close(); } catch(SQLException ex){ throw new RuntimeException("Cannot close connection", ex); } } } public void dataAccessCode() { Connection conn = null; try{ conn = getConnection(); .... } finally{ DBUtil.closeConnection(conn); } } There is no use of having a similar catch block in 100 places in your code. It increases code duplicity which does not help anything. Use template methods for such cases.
29.
28 Document all exceptions inyour application in javadoc Make it a practice to javadoc all exceptions which a piece of code may throw at runtime. Also try to include possible course of action, user should follow in case these exception occur.
30.
29 catch all exceptionsbefore they reach up to the UI You have to catch all exceptions before they reach up to the UI and make your user sad. This means on the "highest level" you want to catch anything that happened further down. Then you can let the user know there was a problem and at the same time take measures to inform the developers, like sending out alarm mails or whatever
31.
references Java exception handlingbest practices by Lokesh Gupta http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/ 15 Best Practices for Exception Handling by Cagdas Basaraner http://codebuild.blogspot.co.uk/2012/01/15-best-practices-about-exception.html 10 Exception handling Best Practices in Java Programming by Javin Paul http://javarevisited.blogspot.co.uk/2013/03/0-exception-handling-best-practices-in-Java-Programming.html