小编Uma*_*hmi的帖子

为什么写Try尝试没有Catch或者最后?

为什么在没有Catch或Finally的情况下编写Try,如下例所示?

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>");
    }

}
Run Code Online (Sandbox Code Playgroud)

java try-with-resources

31
推荐指数
2
解决办法
3万
查看次数

标签 统计

java ×1

try-with-resources ×1