【发布时间】:2014-12-09 00:12:51
【问题描述】:
为什么要像下面的例子那样写 Try without a Catch 或 finally?
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet tryse</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet tryse at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
【问题讨论】:
-
阅读资源尝试(Java 7)
-
重复“在没有“catch”的情况下进行“try-finally”是否有意义?”
-
@Juru:这绝不是那个的复制品......
-
话虽如此,我不认为这是关于 try-with-resources 的第一个问题。不过,+1 仅仅是因为我以前从未听说过此功能!