开发者

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

开发者 https://www.devze.com 2023-03-14 04:38 出处:网络
How would an IOException be thrown in below code ? Is it if the response object times out ? public void doGet(HttpServletRequest request, HttpServletResponse response) {

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

精彩评论

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