java final resource,为什么编写不带catch或Final的Try-With-Resources?

Why write Try without a Catch or Finally as in the following example?

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

out.println("");

out.println("

");

out.println("

Servlet tryse");

out.println("");

out.println("

");

out.println("

Servlet tryse at " + request.getContextPath() + "

");

out.println("");

out.println("");

}

}

解决方案

As explained above this is a feature in Java 7 and beyond. try with resources allows to skip writing the finally and closes all the resources being used in try-block itself. As stated in Docs

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

See this code example

static String readFirstLineFromFile(String path) throws IOException {

try (BufferedReader br =

new BufferedReader(new FileReader(path))) {

return br.readLine();

}

}

In this example the resource is BufferReader object as the class implements the interface java.lang.AutoCloseable and it will be closed whether the try block executes successfully or not which means that you won't have to write br.close() explicitly.

Another important thing to notice here is that if you are writing the finally block yourself and both your try and finally block throw exception then the exception from try block is supressed.

While on the other hand if you are using try-with-resources statement and exception is thrown by both try block and try-with-resources statement then in this case the exception from try-with-resources statement is suppressed.

As the @Aaron has answered already above I just tried to explain you. Hope it helps.

try-with-resourcesJava 7中引入的一种语法结构,用于自动关闭资源。在try-with-resources块中,可以使用分号分隔多个资源的声明。这些资源必须实现AutoCloseable接口或Closeable接口。当try-with-resources块退出时,无论是正常退出还是异常退出,这些资源都会被自动关闭。\[1\] 使用try-with-resources的好处是,不需要手动在finally块中关闭资源,代码更加简洁。同时,try-with-resources语句本身也可以包含catch子句和final子句,它们会在关闭资源之后执行。如果在try-with-resources块中遇到异常,关闭资源的语句会先于catch语句执行。\[2\] 在try-with-resources中声明的资源会在try代码块开始执行之前完成实例化,并且这些资源会被隐式地声明为final。如果在try-with-resources语句中使用了多个资源,关闭资源的顺序将与声明时相反。\[2\] 总结来说,try-with-resources是一种简化资源关闭操作的语法结构,可以替代传统的try-catch-finally块。它能够自动关闭资源,使代码更加简洁和可读。\[3\] #### 引用[.reference_title] - *1* *3* [Java中的Try with Resources语句介绍](https://blog.csdn.net/IndexMan/article/details/117534025)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [学习笔记 · try-resources语句-新的异常处理机制](https://blog.csdn.net/oALiKongZuo/article/details/109462430)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值