Tuesday, May 3, 2011

Why does HttpServlet throw an IOException?

I get why an HttpServlet would throw ServletException, but why IOException? What was the reasoning behind this?

From stackoverflow
  • If the servlet writes to an output stream (i.e. the page) through I/O interfaces, any I/O error will be reported as an IOException.

    Perhaps the API shouldn't be using I/O interfaces directly. It's not uncommon for WebApps using RMI to allow the RMIException to be fall through the servlet interface (it's a subclass of IOException for some reason).

  • I'd suggest that an IOException indicates a problem with processing input/output, e.g. problems reading from the request input, or writing the response, whereas a ServletException has more to do with servlet-specific problems, such as errors regarding servlet provisioning/initialisation, and processing requests.

  • From the docs:

    IOException - if an input or output error is detected when the servlet handles the GET request

    This can happen when you print the servlet output:

    response.getWriter().print() - this method throws IOException

    The socket can be closed before the response finishes to print the output.

0 comments:

Post a Comment