开关资源新方法:Try- with-resources

打印 上一主题 下一主题

主题 851|帖子 851|积分 2555

JDK7新特性:Try- with-resources

try-with-resources 是 JDK 7中引入的一种新的非常处理惩罚机制,它主要用于主动管理资源,可以或许很容易地关闭在 try-catch 语句块中使用的资源。确保资源在不再需要时可以或许被准确关闭。这种机制简化了资源管理,使得资源的开释更加安全和可预测。
resource:是指在程序完成后,必须关闭的对象(例如:文件资源File、IO流、Socket、ServerSocket网络对象、数据库链接对象等)。try-with-resources 语句确保了每个资源在语句结束时关闭。
平常开关资源方法
  1. class Myresources1 implements AutoCloseable {
  2.     @Override
  3.     public void close() throws Exception {
  4.         System.out.println("资源1关闭方法执行");
  5.         throw new Exception("资源1关闭异常");
  6.     }
  7. }
  8. class Myresources2 implements AutoCloseable {
  9.     @Override
  10.     public void close() throws Exception {
  11.         System.out.println("资源2关闭方法执行");
  12.         throw new Exception("资源2关闭异常");
  13.     }
  14. }
复制代码
为了制止在代码实行中出现非常,使用try-catch-finally举行非常捕获
  1.         Myresources1 myresources1 =null;
  2.         Myresources2 myresources2 = null;
  3.         try{
  4.             myresources1 = new Myresources1();
  5.             myresources2 = new Myresources2();
  6.             System.out.println("hello");
  7.         }catch (Exception e){
  8.             e.printStackTrace();
  9.         }finally {
  10.             if (myresources1!=null){
  11.                 try {
  12.                     myresources1.close();
  13.                 }catch (Exception e){
  14.                     e.printStackTrace();
  15.                 }finally {
  16.                     if (myresources2!=null){
  17.                         try {
  18.                             myresources2.close();
  19.                         }catch (Exception e){
  20.                             e.printStackTrace();
  21.                         }
  22.                     }
  23.                 }
  24.             }
  25.         }
复制代码

Try- with-resources控制资源语法:
  1.     try(Myresources1 myresources1 = new Myresources1();
  2.         Myresources2 myresources2 = new Myresources2();){
  3.            System.out.println("hello");
  4.         //int a = 2/0;
  5.        }catch (Exception e){
  6.            e.printStackTrace();
  7.        }
复制代码

处理惩罚规则


  • 凡是实现了AutoCloseable或者Closeable接口的类,在try()里声明该类实例的时候,在try结束后,close方法都会被调用。不管是否出现非常(int i=1/0会抛出非常),try()里的实例都会被调用close方法
![imag
e.png](https://cdn.nlark.com/yuque/0/2024/png/42425790/1722909280844-945838ee-91a2-4a06-9399-647b744396f1.png#averageHue=%231f2125&clientId=ua8faed9b-5ab9-4&from=paste&height=259&id=TKIhf&originHeight=259&originWidth=735&originalType=binary&ratio=1&rotation=0&showTitle=false&size=37054&status=done&style=none&taskId=u35bf969c-e938-4d98-ab98-4df1f077383&title=&width=735)


  • 越晚声明的对象,会越早被close掉。
  • try结束后主动调用的close方法,这个动作会早于finally里调用的方法。

非常抑制问题
  1. Myresources1 myresources1 = null;
  2.         try{
  3.             myresources1 = new Myresources1();
  4.             //算数异常
  5.             System.out.println(10/0);
  6.         }finally {
  7.             if (myresources1!=null)
  8.                 myresources1.close();
  9.         }
  10. //myresources1类
  11. class Myresources1 implements AutoCloseable {
  12.     @Override
  13.     public void close() throws Exception {
  14.         System.out.println("资源1关闭方法执行");
  15.         throw new Exception("资源1关闭异常");
  16.     }
  17. }
复制代码
运行非常打印:

此时可以看到,非常只打印了close()方法的非常,而 10/0 的非常被抑制了
  1.         try(Myresources1 myresources1 = new Myresources1();){
  2.             System.out.println(10/0);
  3.         }
复制代码
运行非常打印:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表