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

公司宣传网站建设住房和城乡建设部网站第九批

公司宣传网站建设,住房和城乡建设部网站第九批,分毫报价小程序,品牌的佛山网站建设本文研究try-with-resources语句的用法。 这是一个声明一个或多个资源的try语句。 资源是一个对象#xff0c;程序完成后必须将其关闭。 try-with-resources语句可确保在语句末尾关闭每个资源。 任何实现java.lang.AutoCloseable或java.io.Closeable接口的对象都可以用作资源。… 本文研究try-with-resources语句的用法。 这是一个声明一个或多个资源的try语句。 资源是一个对象程序完成后必须将其关闭。 try-with-resources语句可确保在语句末尾关闭每个资源。 任何实现java.lang.AutoCloseable或java.io.Closeable接口的对象都可以用作资源。 在尝试使用资源 在Java 7之前处理SQL语句或ResultSet或Connection对象或其他IO对象之前必须显式关闭资源。 所以一个人会写类似 try{//Create a resource- R } catch(SomeException e){//Handle the exception } finally{//if resource R is not null thentry{//close the resource}catch(SomeOtherException ex){} } 我们必须显式关闭资源从而添加更多代码行。 在极少数情况下开发人员会忘记关闭资源。 因此为了克服这些问题和其他问题Java 7中引入了try-with-resources 。 让我们看一个示例说明如何在Java 7之前的版本中使用try..catch…。让我创建2个自定义异常-ExceptionA和ExceptionB。 这些将在整个示例中使用。 public class ExceptionA extends Exception{public ExceptionA(String message){super(message);} }public class ExceptionB extends Exception{public ExceptionB(String message){super(message);} } 让我们创建一些资源例如OldResource它有两种方法– doSomeWork完成一些工作close完成关闭。 请注意这描述了通用资源的使用-做一些工作然后关闭资源。 现在每个操作doSomeWork和close都会引发异常。 public class OldResource{public void doSomeWork(String work) throws ExceptionA{System.out.println(Doing: work);throw new ExceptionA(Exception occured while doing work);}public void close() throws ExceptionB{System.out.println(Closing the resource);throw new ExceptionB(Exception occured while closing);} } 让我们在示例程序中使用此资源 public class OldTry {public static void main(String[] args) {OldResource res null;try {res new OldResource();res.doSomeWork(Writing an article);} catch (Exception e) {System.out.println(Exception Message: e.getMessage() Exception Type: e.getClass().getName());} finally{try {res.close();} catch (Exception e) {System.out.println(Exception Message: e.getMessage() Exception Type: e.getClass().getName());}}} } 输出 Doing: Writing an article Exception Message: Exception occured while doing work Exception Type: javaapplication4.ExceptionA Closing the resource Exception Message: Exception occured while closing Exception Type: javaapplication4.ExceptionB 该程序很简单创建一个新资源使用它然后尝试关闭它。 可以看看那里多余的代码行数。 现在让我们使用Java 7的try-with-resource结构实现相同的程序。 为此我们需要一个新资源– NewResource。 在Java 7中新接口为java.lang.AutoCloseable 。 那些需要关闭的资源将实现此接口。 所有较旧的IO API套接字API等都实现了Closeable接口-这意味着可以关闭这些资源。 使用Java 7 java.io.Closeable实现AutoCloseable 。 因此一切正常而不会破坏任何现有代码。 下面的NewResource代码 public class NewResource implements AutoCloseable{String closingMessage;public NewResource(String closingMessage) {this.closingMessage closingMessage;}public void doSomeWork(String work) throws ExceptionA{System.out.println(work);throw new ExceptionA(Exception thrown while doing some work);}public void close() throws ExceptionB{System.out.println(closingMessage);throw new ExceptionB(Exception thrown while closing);}public void doSomeWork(NewResource res) throws ExceptionA{res.doSomeWork(Wow res getting res to do work);} } 现在使用try-with-resource在示例程序中使用NewResource public class TryWithRes {public static void main(String[] args) {try(NewResource res new NewResource(Res1 closing)){res.doSomeWork(Listening to podcast);} catch(Exception e){System.out.println(Exception: e.getMessage() Thrown by: e.getClass().getSimpleName());}} } 输出 Listening to podcast Res1 closing Exception: Exception thrown while doing some work Thrown by: ExceptionA 上面要注意的一件事是关闭方法抛出的异常被worker方法抛出的异常所抑制。 因此您可以立即注意到这两种实现之间的差异一种实现最终使用try ... catch ...另一种使用try-with-resource。 在上面的示例中仅一个资源被声明为已使用。 一个人可以在try块中声明和使用多个资源也可以嵌套这些try-with-resources块。 随之在java.lang.Throwable类中添加了一些新方法和构造函数所有这些方法都与抑制与其他异常一起抛出的异常有关。 最好的例子是-try块抛出的ExceptionA会被finally关闭资源时抛出的ExceptionB抑制这是Java 7之前的行为。 但是对于Java 7抛出的异常会跟踪它在被捕获/处理的过程中被抑制的异常。 因此前面提到的示例可以重新陈述如下。 由close方法抛出的ExceptionB被添加到由try块抛出的ExceptionA的抑制异常列表中。 让我用以下示例说明您嵌套的try-with-resources和Suppressed异常。 嵌套的尝试资源 public class TryWithRes {public static void main(String[] args) {try(NewResource res new NewResource(Res1 closing);NewResource res2 new NewResource(Res2 closing)){try(NewResource nestedRes new NewResource(Nestedres closing)){nestedRes.doSomeWork(res2);}} catch(Exception e){System.out.println(Exception: e.getMessage() Thrown by: e.getClass().getSimpleName());}} } 上面的输出将是 Wow res getting res to do work Nestedres closing Res2 closing Res1 closing Exception: Exception thrown while doing some work Thrown by: ExceptionA 注意关闭资源的顺序最新的优先。 还要注意抑制了每个close操作引发的异常。 让我们看看如何检索被抑制的异常 禁止的异常 public class TryWithRes {public static void main(String[] args) {try(NewResource res new NewResource(Res1 closing);NewResource res2 new NewResource(Res2 closing)){try(NewResource nestedRes new NewResource(Nestedres closing)){nestedRes.doSomeWork(res2);}} catch(Exception e){System.out.println(Exception: e.getMessage() Thrown by: e.getClass().getSimpleName());if (e.getSuppressed() ! null){for (Throwable t : e.getSuppressed()){System.out.println(t.getMessage() Class: t.getClass().getSimpleName());}}}} } 上面代码的输出 Wow res getting res to do work Nestedres closing Res2 closing Res1 closing Exception: Exception thrown while doing some work Thrown by: ExceptionA Exception thrown while closing Class: ExceptionB Exception thrown while closing Class: ExceptionB Exception thrown while closing Class: ExceptionB getSuppressed方法用于检索被抛出的异常阻止的异常。 还向Throwable类添加了新的构造函数该构造函数可用于启用或禁用异常抑制。 如果禁用则不会跟踪任何抑制的异常。 参考 Java 7项目硬币 Try -with-resources以及我们的JCG合作伙伴 Mohamed Sanaulla在Experiences Unlimited Blog 上的示例进行了解释 。 相关文章 速览Java 7 MethodHandle及其用法 JDK中的设计模式 了解和扩展Java ClassLoader Java内存模型–快速概述和注意事项 翻译自: https://www.javacodegeeks.com/2011/07/java-7-try-with-resources-explained.html
http://www.zqtcl.cn/news/337878/

相关文章:

  • 哪些网站是做外贸生意的网站建设所需美工
  • 网站建设哪个公司比较好惠州网络问政平台
  • 河南网站备案系统短信广州注册公司程序
  • 苏晋建设集团网站跨专业的简历怎么制作
  • 交互网站怎么做设计师作品网站
  • 国外网站的分析工具有哪些办公室装修计入什么会计科目
  • 手机网站 需求模板3000元建设个人网站
  • 请人做网站域名和主机thinkphp网站开发实战教程
  • 做地产网站哪家好饮料网站建设价格
  • 外管局网站 报告怎么做wordpress 阿里
  • 湘潭做网站 去磐石网络山西自助建站费用低
  • 温州哪里做网站比较好昆明网页制作开发
  • 网站建设淘宝客网站建设与网页设计入门
  • 网站推广营销联系方式俄语免费网站制作
  • 广东企业网站seo点击软件搭建本地网站
  • 商丘做网站的价格专业网站制作哪家强
  • 瑞安微信网站软件公司网站设计与制作
  • 片头网站网站建设服装在线商城实训报告
  • wordpress做企业网站怎样做网页推广
  • 网站建设售后服务安全维护企业网站开发 外文文献
  • 网站设计英文翻译系统开发的五个阶段
  • 成华区门户网站拍卖网站开发多少钱
  • html设计网站wordpress 评论增加字段
  • 搭建正规网站小程序开发难不难
  • 做静态网站用什么软件自己编写代码建设微网站
  • 备案网站ipoa系统主要干什么的
  • 杭州专业网站建设在哪里wordpress主题重置
  • 仿wordpress站赣州专业网站推广
  • 网站开发需要多长时间python链接wordpress
  • 网上交易网邯郸网站seo