Exception handling

There are two types of exceptions:

  • Checked exceptions: When a service method throws this exception, all the consumer methods should either handle or throw the exception
  • Unchecked exceptions: The consumer method is not required to handle or throw the exception thrown by the service method

RuntimeException and all its subclasses are unchecked exceptions. All other exceptions are checked exceptions.

Checked exceptions can make your code cumbersome to read. Take a look at the following example:

    PreparedStatement st = null;    try {        st = conn.prepareStatement(INSERT_TODO_QUERY);        st.setString(1, bean.getDescription());        st.setBoolean(2, bean.isDone());        st.execute();        } catch (SQLException e) { logger.error("Failed : " + INSERT_TODO_QUERY, ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.