As a general rule, Throwable should never be thrown or extended.  The danger of catching Throwable is that code may never exit when it is supposed to (eg. On thread shutdown), or that problems that need to be resolved will be hidden.  One of the unfortunate side effects of the name of Throwable is that it sounds like it is safe to throw.

Trap: Extend Exception, RuntimeException, Error, or subclasses thereof.  Never throw or extend Throwable.

But there are some valid reasons to catch Throwable:

  • in thread loops that must run for ever, to ensure all Exceptions and Errors are caught and the thread resumes.  Perhaps it will execute correctly next loop.

  • in base framework code where users need to be protected from errors screens which may cause confusion and panic.  eg. Users should probably see a “System unavailable due to scheduled maintenance” message rather than a DatabaseUnavailableException.

Tip: Throwable is a real class, not an interface.

Throwable exceptions are often missed by those writing framework code, even when the code is designed to be used by others.  An accidental Throwable or Error can be swallowed up by frameworks because they only catch Exception, or logging is not integrated – so expect the worst and catch Throwable in the main entry points for the software.  For example, if there is a BaseServlet class that is extended by all other Servlets on the project, put a catch for Throwable around the entry point to the subclasses.

public class BaseServlet extends HttpServlet
{
    public void doProcessing(HttpServletRequest request,  
                             HttpServletResponse response)           throws ServletException,  
                   IOException   {       response.setHeader("Pragma", "No-cache");       response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        try       {           process(request, response, httpSession, user);       }       catch (Throwable t)
        {           log(t.getMessage(), t);
            response.sendRedirect(UNEXPECTED_ERROR_URL);       }   }
}

There are some classic Throwable conditions that can occur which are unexpected, but most are caused by developers who have mistakenly extended Throwable instead of Exception.

Tip: There is no need to declare Throwable since it is unchecked.

blog comments powered by Disqus