属于Java7的新特性。
经常会用try-catch来捕获有可能抛出异常的代码。如果其中还涉及到资源的使用的话,最后在finally块中显示的释放掉有可能被占用的资源。
但是如果资源类已经实现了AutoCloseable这个接口的话,可以在try()括号中可以写操作资源的语句(IO操作),会在程序块结束时**自动释放掉占用的资源**,不用再在finally块中手动释放了。

例子:
没使用该特性

如果要释放的资源多得话,判断加catch占得篇幅大,不美观也不精简
用了这个特性后代码可以精简些

try-catch-finally

try-with-resources

----------------------------------------------------分割线---------------------------------------------------------------------
取自:https://jenkov.com/tutorials/java-exception-handling/try-with-resources.html
Using Multiple Resources
You can use multiple resources inside a Java try-with-resources block and have them all automatically closed. Here is an example of using multiple resources inside a try-with-resources block:

This example creates two resources inside the parentheses after the try keyword. An FileInputStream and a BufferedInputStream. Both of these resources will be closed automatically when execution leaves the try block.
Closing Order
The resources declared in a Java try-with-resources construct will be closed in reverse order of the order in which they are created / listed inside the parentheses. In the example in the previous section, first the will be closed, then the FileInputStream.
Custom AutoClosable Implementations
The Java try-with-resources construct does not just work with Java’s built-in classes. You can also implement the java.lang.AutoCloseable interface in your own classes, and use them with the try-with-resources construct.

The AutoClosable interface only has a single method called close(). Here is how the interface looks:

Any class that implements this interface can be used with the Java try-with-resources construct. Here is a simple example implementation:

The doIt() method is not part of the AutoClosable interface. It is there because we want to be able to do something more than just closing the object.

Here is an example of how the MyAutoClosable is used with the try-with-resources construct:

Here is the output printed to System.out when the method myAutoClosable() is called:

As you can see, try-with-resources is a quite powerful way of making sure that resources used inside a try-catch block are closed correctly, no matter if these resources are your own creation, or Java’s built-in components.

举例

参考:
Java必须懂的try-with-resources

java知识大全

jenkov.com

try(){}用法try-with-resources、try-catch-finally相关推荐

  1. java try的用法_Java中try、catch的使用方法

    Java中有两种处理异常的方式,分别是用throws抛出异常.用try.catch捕获异常. try-catch 在Java try-catch语句的语法格式: try{ //代码块 }catch(E ...

  2. java try 的用法_java try(){}catch(){}自动释放资源及用法

    java语言中try(){}catch(){}的用法介绍.介绍了通常的try-catch的用法以及 try-with-resources 的用法. 1.try{} catch(){}的用法 通常try ...

  3. Java里try catch的简单用法

    Java里try catch的简单用法: Java里try catch的简单用法: 1.try+catch 程序的流程是:运行到try块中,如果有异常抛出,则转到catch块去处理.然后执行catch ...

  4. C++:try catch语句用法

    C++中try catch语句用法有非常灵活,有许多种组合: 这里简单介绍以下最基本的用法: 1. 格式 try{ ... } catch{ ... } try语句块是用来判断是否有异常: catch ...

  5. java中 try用法,Java里try catch的简单用法

    优质回答 回答者:temps1991 Java里try catch的简单用法: 1.try+catch 程序的流程是:运行到try块中,如果有异常抛出,则转到catch块去处理.然后执行catch块后 ...

  6. C++ 异常,标准异常类,自定义异常类,throw,try,catch语句

    要学习异常,首先就要学习throw和try,catch语句: C++中try catch语句用法有非常灵活,有许多种组合: 这里简单介绍以下最基本的用法: 1. 格式 try{ ... } catch ...

  7. 深度理解Android InstantRun原理以及源码分析

    @Author 莫川 Instant Run官方介绍 简单介绍一下Instant Run,它是Android Studio2.0以后新增的一个运行机制,能够显著减少你第二次及以后的构建和部署时间.简单 ...

  8. Tomcat 7 自动加载类及检测文件变动原理

    在一般的 web 应用开发里通常会使用开发工具(如 Eclipse.IntelJ )集成 tomcat ,这样可以将 web 工程项目直接发布到 tomcat 中,然后一键启动.经常遇到的一种情况是直 ...

  9. (4.2.32)各大热补丁方案分析和比较

    选自: [腾讯bugly干货分享]微信Android热补丁实践演进之路 各大热补丁方案分析和比较 继插件化后,热补丁技术在2015年开始爆发,目前已经是非常热门的Android开发技术.其中比较著名的 ...

  10. Activity启动流程:Hook实现启动未注册Activity

    Android中插件化的简单实现:启动未注册的Activity 前言 本文介绍在Android中启动未在AndroidManifest中注册的Activity的一个解决方案.主要需要掌握以下知识点: ...

最新文章

  1. eclipse 开发常见问题集锦
  2. 计算机教 学计划书,计算机教师工作计划个人范例
  3. xdeepfm算法思维导图与代码
  4. DataFormatString={0:格式字符串} 用法
  5. 用yum装程序 报[Errno 12] Timeout on Trying other mirror.
  6. 各种流行编程语言的优缺点
  7. [AHOI2004]实验基地 dp+枚举
  8. activity 、window与view的关系 (上)
  9. 对WITH和from(select ...)的一点比较
  10. Java8 Stream + +很不错的文章集合
  11. 9、两个栈实现队列(Python)
  12. 数据类型 --生成器
  13. Shopee平台有哪些电商大促活动?大促活动如何报名?
  14. VBA小程序_对于选中的单元格进行取消合并_选择空值向上填充
  15. 已拦截跨源请求:同源策略禁止读取位于...的远程资源。(原因:CORS 请求未能成功)。
  16. ps2口键盘改usb计算机设置,PS2接口键盘、鼠标改为USB接口
  17. netty传输html页面,java 文件传输(netty) 实例源码
  18. 推荐系统CTR技术的演变历史
  19. 模拟登陆广西科技大学正方教务系统
  20. 【相机标定】相机内参

热门文章

  1. 【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI
  2. 数字调制系统工作原理_无人值守道闸系统的工作原理
  3. ABAP-串口通信-道闸设备
  4. 视频设置新封面,快速给视频置入图片的方法
  5. 小白项目初尝试——全民飞机大战初期
  6. 一个人可以有多少个朋友
  7. surface计算机显示桌面,带你玩转Surface!你应该记住的快捷键大全
  8. 2022年最新微信小程序支付后端代码通俗易懂一篇文章让你了解微信支付
  9. 电子商务网站功能模块汇总
  10. ORA-27072 instance crash