当前位置: 首页 > news >正文

衡水建设局网站首页广东佛山疫情最新情况

衡水建设局网站首页,广东佛山疫情最新情况,武汉软件开发公司,新网互联 网站上传属于Java7的新特性。 经常会用try-catch来捕获有可能抛出异常的代码。如果其中还涉及到资源的使用的话,最后在finally块中显示的释放掉有可能被占用的资源。 但是如果资源类已经实现了AutoCloseable这个接口的话,可以在try()括号中可以写操作资源的语句(…

在这里插入图片描述

属于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

http://www.jinmujx.cn/news/110380.html

相关文章:

  • 南充网站建设工作室百度pc版网页
  • 动态ip地址做网站域名注册信息查询
  • 官方网站建设银行年利息是多少互联网营销师是哪个部门发证
  • 无锡自助建网站google关键词排名
  • 免费奖励代码网站如何创建一个网页
  • 常州便宜的做网站服务武汉关键词排名工具
  • 优惠网站怎么做app推广注册从哪里接单
  • 网站开发实例 csdn国际重大新闻事件2023
  • 美国网站 香港ip苹果aso优化
  • 拍摄广告片制作公司关键词优化公司网站
  • 江门网站建设南京seo优化
  • 各种网站底部图标代码培训网站搭建
  • 深圳华强北手表各品牌批发湖北搜索引擎优化
  • 设计网站首页要几天网站秒收录工具
  • 制作手机端网站开发企业网站制作费用
  • 上杭网站定制关键词排名监控批量查询
  • 常州优化网站站长工具友链查询
  • 介休网站建设为企业策划一次网络营销活动
  • 厦门的网站建设公司潍坊关键词优化软件
  • 百度怎么搜索到自己的网站百度搜索推广创意方案
  • 网站建设综合设计营销型网站制作建设
  • 电商平台哪个好做东莞seo建站咨询
  • 做简单鬼畜视频的网站优化营商环境条例全文
  • 做哪些网站好吉林seo关键词
  • 网站做优化得话从哪里优化接app推广
  • 网站正在建设中亚洲最新社会舆情信息
  • 新网网站建设关于seo如何优化
  • 网上哪个网站教做西点国外外链平台
  • 怎样看出一个网站是那个公司做的天津的网络优化公司排名
  • 网站建设项目详情网站的优化seo