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.
精彩评论