详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp82

 

今天主要研究Java se 7中异常处理的新功能.从今天开始正在将jdk7的说法改为java se 7跟oracle官网的一致

一、新增了try-with-resource 异常声明

在JDK7中只要实现了AutoCloseable或Closeable接口的类或接口,都可以使用try-with-resource来实现异常处理和资源关闭。下面做一下

JDK7以前:

static String readFirstLingFromFile(String path) throws IOException{

  BufferedReader br=null;

  try{

  br=new BufferedReader(new FileReader(path));

  return br.readLine();

  }catch(IOException e){
   e.printStackTrace();

  }finally{
   if(br!=null)
     br.close();
  }

  return null;

  }

JDK7 及以后版本:

static String readFirstLineFromFile(String path) throws IOException {
  try (BufferedReader br = new BufferedReader(new FileReader(path))) {
    return br.readLine();
  }
}

通过以上对比我们可以发现具有以下优点:

1、代码更精练。在Java se 7以前版都有finally块,如果使用一些框架可能会将finally块交由框架处理,如spring。在jdk7及后的版本,只要资源类实现了AutoCloseable或Closeable程序在执行宛try块后会自动close所使用的资源无论br.readLine()是否抛出异常。我估计针对jdk7像spring这些框架也会做出一些比较大的调整。

 

2、代码更完全。在出现资源泻漏的程序中,很多情况是开发人员没有或没有正确地关闭资源导致的。JDK7后采用try-with-resource的方式,则可以将资源关闭这种与业务实现没有很大直接关系的工作交给JVM完成。省去了部分开发中可能出现的代码风险。

 

 异常抛出顺序。在Java se 7中的try-with-resource机制中异常的抛出顺序与Java se 7以前的版本有一点不一样。

JDK7以前如果rd.readLine()与rd.close()(在finally块中)都抛出异常则只会抛出finally块中的异常,不会抛出rd.readLine();中的异常。这样经常会导致得到的异常信息不是调用程序想要得到的。

JDK7及以后版本中如果采用try-with-resource机制,如果在try-with-resource声明中抛出异(可能是文件无法打或都文件无法关闭)同时rd.readLine();也势出异常,则只会势出rd.readLine()的异常。

 

try-with-resource可以声明多个资源。下面的例子是在一个zip文件中检索文件名并将检索后的文件名存入一个txt文件中:

 

public static void writeToFileZipFileContents(String zipFileName, String outputFileName)
    throws java.io.IOException {

    java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII");
    java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName);

    // Open zip file and create output file with try-with-resources statement

    try (
      java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
      java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
    ) {

      // Enumerate each entry

      for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) {

        // Get the entry name and write it to the output file

        String newLine = System.getProperty("line.separator");
        String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine;
        writer.write(zipEntryName, 0, zipEntryName.length());
      }
    }
  }

上面的例子,无论正常执行还是有异常抛出,zf和writer都会被执行close()方法。不过需要注意的是在JVM里调用的顺序是与声明的顺序相反。在JVM里调用的顺序为:

writer.close();

zf.close();

所在在使用时一定要注意。

声明资源时要分析好资源关闭顺序。

JDK7及后的版本的JDBC也支持try-with-resource.如下示例:

 public static void viewTable(Connection con) throws SQLException {

    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";

    try (Statement stmt = con.createStatement()) {

      ResultSet rs = stmt.executeQuery(query);

      while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        System.out.println(coffeeName + ", " + supplierID + ", " + price +
                           ", " + sales + ", " + total);
      }

    } catch (SQLException e) {
      JDBCTutorialUtilities.printSQLException(e);
    }
  }

在try-with-resource中也可以有catch与finally块。只是catch与finally块是在处理完try-with-resource后才会执行。

二、catch多种异常并抛出新的异常

1、catch多种异常抛出一种异常

在JDK7以前大家会经常处理类似下面的代码:

try{

.........

}catch (IOException ex) {
     logger.log(ex);
     throw new SpecialException();
catch (SQLException ex) {
     logger.log(ex);
     throw new SpecialException();
}

这处理导致代码非常的难看,和许多重复的代码。JDK7后,大家可以这样处理:

try{

.........

}catch (IOException | SQLException ex) {
     logger.log(ex);
     throw new SpecialException();
}

注:在上面例子中的ex是隐式的final不可以在catch块中改变ex.

2、Rethrowing Exceptions with More Inclusive Type Checking

在Java se 7以前的版本。在方法声明中声明抛出的异常如果在方法体内没有抛出是不允许的。如下示例:

static class FirstException extends Exception { }
  static class SecondException extends Exception { }

  public void rethrowException(String exceptionName) throws Exception {
    try {
      if (exceptionName.equals("First")) {
        throw new FirstException();
      } else {
        throw new SecondException();
      }
    } catch (Exception e) {
      throw e;
    }
  }

上面这种代码其实很多开发人员都不喜欢,有的可能会提出一些改进方法,但无法做到非常精准的方法异常声明。下面看一下Java se 7的做法:

 public void rethrowException(String exceptionName)
  throws Exception, FirstException, SecondException {
    try {
      // ...
    }
    catch (Exception e) {
      throw e;
    }
  }

异常处理这一块,Java se7改进了许多,开发人员需要认真的学习和试验。

今天先谈到这里。接下来将谈一下下划线在数字表示中的应用和泛型的改进。

[转] Java se 7新特性研究(二)的更多相关文章

  1. Java SE 6 新特性: Java DB 和 JDBC 4.0

    http://www.ibm.com/developerworks/cn/java/j-lo-jse65/index.html 长久以来,由于大量(甚至几乎所有)的 Java 应用都依赖于数据库,如何 ...

  2. Java SE 6 新特性: 对脚本语言的支持

    2006 年底,Sun 公司发布了 Java Standard Edition 6(Java SE 6)的最终正式版,代号 Mustang(野马).跟 Tiger(Java SE 5)相比,Musta ...

  3. Java SE 6 新特性: HTTP 增强--转

    概述 Java 语言从诞生的那天起,就非常注重网络编程方面的应用.随着互联网应用的飞速发展,Java 的基础类库也不断地对网络相关的 API 进行加强和扩展.在 Java SE 6 当中,围绕着 HT ...

  4. Java SE 6 新特性: 编译器 API

    新 API 功能简介 JDK 6 提供了在运行时调用编译器的 API,后面我们将假设把此 API 应用在 JSP 技术中.在传统的 JSP 技术中,服务器处理 JSP 通常需要进行下面 6 个步骤: ...

  5. DynamicMBean(Java SE 6 新特性: JMX 与系统管理)

    Dynamic MBean 是一种在运行时定义其管理接口的 MBean.例如,配置 MBean 可以通过解析 XML 文件来确定它所公开的属性名称和类型. 任何实现 DynamicMBean 接口的类 ...

  6. [转]Java se 7 最新特性研究(一)

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp81   从2006到现在等待了多年的jdk7终于发布了.这里将对它的一些 ...

  7. java 1.8新特性(二) 关于 function 接口的使用

    需求1:从user集合中 找出age=15的用户  传统方法 就不说了 举例明一下 java1.8 使用Predicate接口解决该需求: @FunctionalInterface public in ...

  8. Java SE 14 新增特性

    Java SE 14 新增特性 作者:Grey 原文地址:Java SE 14 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...

  9. Java SE 15 新增特性

    Java SE 15 新增特性 作者:Grey 原文地址:Java SE 15 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...

随机推荐

  1. Spring核心--IOC

    先说说Spring框架 Spring框架 Spring框架的作用 Spring 的核心(IOC功能) SpringAOP功能 原有的业务流程:请求->servlet-->service-- ...

  2. css中的几个小tip(一)

    原博:http://www.webhek.com/post/7-tips-web-front-developer-must-know__trashed.html 1.元素的margin-top.mar ...

  3. POJ 2386 Lake Counting (简单深搜)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  4. CodeForces 816B Karen and Coffee(前缀和,大量查询)

    CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...

  5. JUnit4在Eclipse中的使用

    测试是项目开发中很重要的一环.实际上,建议的项目前期编写过程是:构思-> 编写单元测试代码-> 编写接口->编写实现类-> 测试实现类->编写主类....JUnit是一个 ...

  6. 运用经典方法进行横截面数据分类 笔记 (基于R)

    参考资料: [1]吴喜之. 复杂数据统计方法[M]. 中国人民大学出版社, 2015. 一.logistic回归与probit回归 logistic回归和probit回归都属于广义线性模型. 广义线性 ...

  7. Strace跟踪解决expect乱码问题

    --Strace跟踪解决expect乱码问题 ----------------------------------2014/07/27 情景:需要在本机抓去另外一台远程数据库中的数据. 执行语句:./ ...

  8. RabbitMQ --- Publish/Subscribe(发布/订阅)

    目录 RabbitMQ --- Hello Mr.Tua RabbitMQ --- Work Queues(工作队列) 前言 在第二篇文章中介绍了 Work Queues(工作队列),它适用于把一个消 ...

  9. 使用Python写一个贪吃蛇

    参考代码http://blog.csdn.net/leepwang/article/details/7640880 我在程序中加入了分数显示,三种特殊食物,将贪吃蛇的游戏逻辑写到了SnakeGame的 ...

  10. WebService/WCF/WebAPI区别

    详细描述它们之间的区别,为什么这么选择,尤其是WCF vs WebAPI