SQLException … RuntimeException NullPointerException IndexOutOfBoundsException … must be caught must be declared recoverable errors should be caught could be declared recoverable errors should not be caught could be declared irrecoverable errors
at com.example.myproject.OpenSessionInViewFilter.doFilter at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter at com.example.myproject.ExceptionHandlerFilter.doFilter ... 22 more Caused by: com.example.myproject.MyProjectServletException at com.example.myproject.MyServlet.doPost at javax.servlet.http.HttpServlet.service at javax.servlet.http.HttpServlet.service at org.mortbay.jetty.servlet.ServletHolder.handle at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter at com.example.myproject.OpenSessionInViewFilter.doFilter ... 27 more Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [com.example.myproject.MyEntity] at org.hibernate.exception.SQLStateConverter.convert at org.hibernate.exception.JDBCExceptionHelper.convert at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert at org.hibernate.persister.entity.AbstractEntityPersister.insert ... 32 more Caused by: java.sql.SQLException: Violation of unique constraint MY_ENTITY_UK_1: duplicate value(s) for column(s) MY_COLUMN in statement [...] at org.hsqldb.jdbc.Util.throwError at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert ... 54 more Root cause Wrappings Thrown exception
at com.example.myproject.OpenSessionInViewFilter.doFilter at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter at com.example.myproject.ExceptionHandlerFilter.doFilter ... 22 more Caused by: com.example.myproject.MyProjectServletException at com.example.myproject.MyServlet.doPost at javax.servlet.http.HttpServlet.service at javax.servlet.http.HttpServlet.service at org.mortbay.jetty.servlet.ServletHolder.handle at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter at com.example.myproject.OpenSessionInViewFilter.doFilter ... 27 more Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [com.example.myproject.MyEntity] at org.hibernate.exception.SQLStateConverter.convert at org.hibernate.exception.JDBCExceptionHelper.convert at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert at org.hibernate.persister.entity.AbstractEntityPersister.insert ... 32 more Caused by: java.sql.SQLException: Violation of unique constraint MY_ENTITY_UK_1: duplicate value(s) for column(s) MY_COLUMN in statement [...] at org.hsqldb.jdbc.Util.throwError at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert ... 54 more Root cause Wrappings Thrown exception signalers
manual filtering 127,456 issues 1,963 with stack traces 2,542 repositories 589 with stack traces 482 after manual filtering 31,592 issues 4,042 with stack traces Search GHTorrent and Google Code for android 6,005 issues with stack traces from 539 projects
try { // various operations } catch (AppSpecificException e) { // deal with app exception } catch (Throwable t) { throw new RuntimeException("Error thrown") } } OutOfMemory caught Typical occurence • “Catch all” exception blocks • Also in the core library! Bad because Wraps irrecoverable errors in errors that can be recovered from leading to inconsistent state
{ throw new IOException("Error"); } catch (Exception e) { throw new RuntimeException("Error"); } } Runtime exception wrapping a checked exception No need to throw RunTimeException Typical occurence Methods that “swallow” exceptions to avoid changing the signature Bad because Recoverable conditions are hidden and not treated at the place they occurred Occurrences 50% of the inappropriate wrappings
// various operations throw new AppSpecificException("Error"); } Typical occurence static initializers Bad because Raises the severity of bening exceptions and may lead to non-handling of problems that may have been recoverable ExceptionInInitializerError thrown
recoverable conditions • Do not catch Errors • Only throw exceptions that precisely define recoverable conditions • All (checked and unchecked) exceptions thrown in libraries should be documented
analysis tools • Java >= 8 • Use @Nullable • Encode nullability or possibility of exceptions in types • Optional: Either a result or null (in stdlib) • Try: Either an exception or a result (not in stdlib)