规约内容:json
2.1 异常处理 6. 【强制】 finally 块必须对资源对象、流对象进行关闭,有异常也要作 try - catch 。 说明:若是 JDK 7 及以上,可使用 try - with - resources 方式。
jdk 7 以前:curl
@Test public void test1() throws IOException { String filepath = "D:\\gui-config.json"; BufferedReader br = null; String curline; try { br = new BufferedReader(new FileReader(filepath)); while ((curline = br.readLine()) != null) { System.out.println(curline); } } catch (FileNotFoundException e) { throw new FileNotFoundException("文件xxx不存在,请检查后重试。"); // trturn xxx; } catch (IOException e) { throw new IOException("文件xxx读取异常。"); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { throw new IOException("关闭流异常。"); } } }
jdk 7 及以上ui
@Test public void test2() throws IOException { String filepath = "D:\\gui-config.json"; try ( FileReader fileReader = new FileReader(filepath); BufferedReader br = new BufferedReader(fileReader) ) { String curline = null; while ((curline = br.readLine()) != null) { System.out.println(curline); } } }