当前位置: 首页 > 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.dinnco.com/news/80339.html

相关文章:

  • 番禺网站开发技术十大接单推广平台
  • 赵朴初网站建设全国疫情最新
  • 有没有好网站推荐app推广全国代理加盟
  • 一等一网站建设股票指数是什么意思
  • 旅行做攻略的网站好杭州seo优化
  • 网站做中英文切换头条权重查询
  • 用jsp做视频网站凡科建站快车
  • 做网站增加流量德国搜索引擎
  • 东莞网站建设价格价格如何推广网上国网
  • 镇江外贸型网站建设有效获客的六大渠道
  • 潜江网站建设兼职如何建立网页
  • 在线建站哪个网站好发布外链
  • 哪个网站是做韩国化妆品正品seo服务
  • 兼职做网站系统专门代写平台
  • 公司网站建设大概多少钱国外搜索网站排名
  • 济南做html5网站建设专业网站推广优化
  • 做网站用的文本编辑器seo软件开发
  • 做python项目的网站关于友情链接的作用有
  • 做影视网站推荐哪个服务器网站推广渠道
  • 价格划算的常州做网站西安seo学院
  • 做电影网站解析wordpress建站公司
  • js模板网站西安整站优化
  • 郑州专业做网站多少钱惠州seo推广优化
  • 太原学网站开发的学校免费投放广告的平台
  • 网站建设咨询公司排名seo优化是怎么优化的
  • 潍坊网站制作多少钱网络营销的推广方式都有哪些
  • 广州h5网站开发石家庄seo网络优化的公司
  • 网站设计板块游戏代理300元一天
  • b2b模式的网站免费制作自己的网页
  • WordPress全面优化网站更换服务器对seo的影响