开发者

Why does response.getWriter() throw a checked exception?

How would an IOException be thrown in below code ? Is it if the response object times out ?

public void doGet(HttpServletRequest request, HttpServletResponse response) {
            try {
                response.getWriter().print("Test");
            } catch (I开发者_高级运维OException e) {
                e.printStackTrace();
            }
}


You are trying to write to a socket, so there could be all sorts of IO errors. The socket could have been closed / reset for example.


getWriter() javadoc

"IOException - if an input or output exception occurred"

In short, getWriter is an input/output operation which tries to open a PrintWriter (I believe). The opening of that writer can simply fail, resulting in the IOException thrown.

Plus, the print() operation is also input/output, so that goes by the same conditions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜