A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

这里面有两个main方法,实现的功能是一模一样的,不过一个有try catch,一个没有,我想问 这个try catch有没有必要,面试的时候需要写不
  1. public static void main(String[] args) {

  2.                 FileInputStream fis = null;
  3.                 FileOutputStream fos = null;
  4.                 try {
  5.                         fis = new FileInputStream("e:\\a.txt");
  6.                         fos = new FileOutputStream("e:\\b.txt");
  7.                         byte[] buf = new byte[fis.available()];   //创建了一个关联文件大小一样的缓冲区
  8.                                 fis.read(buf);
  9.                                 Arrays.sort(buf);
  10.                                 fos.write(buf);
  11.                         fos.flush();
  12.                 } catch (IOException e) {
  13.                         System.out.println(e.toString());
  14.                 } finally {
  15.                         try {
  16.                                 fis.close();
  17.                         } catch (IOException e) {
  18.                                 System.out.println(e.toString());
  19.                         }
  20.                         try {
  21.                                 fos.close();
  22.                         } catch (IOException e) {
  23.                                 System.out.println(e.toString());
  24.                         }
  25.                 }
  26.         }


  27. public static void main(String[] args) throws IOException {
  28.                 FileInputStream fis = new FileInputStream("e:\\a.txt");
  29.                 FileOutputStream fos = new FileOutputStream("e:\\b.txt");
  30.                 byte[] buf = new byte[fis.available()];
  31.                 fis.read(buf);
  32.                 Arrays.sort(buf);
  33.                 fos.write(buf);
  34.                 fis.close();
  35.                 fos.close();
  36.         }
复制代码


这里面有两段

10 个回复

倒序浏览
这个东西,听老师讲第一种只有面试的时候会用到,第二种才是开发用的
回复 使用道具 举报
empty3717 来自手机 中级黑马 2016-7-16 12:13:54
藤椅
第二种清晰明了。
回复 使用道具 举报
cat73 黑马帝 2016-7-16 12:16:14
板凳
本帖最后由 cat73 于 2016-7-16 12:28 编辑

我推荐这种写法(Java7 以上可用):
  1. try(InputStream fis = new FileInputStream("e:\\a.txt")) {
  2.     try(OutputStream fos = new FileOutputStream("e:\\b.txt")) {
  3.         byte[] buf = new byte[fis.available()];
  4.         fis.read(buf);
  5.         Arrays.sort(buf);
  6.         fos.write(buf);
  7.     }
  8. } catch(IOException e) {
  9.     e.printStackTrace();
  10.     // 善后处理 如给用户提示出现了错误
  11. }
复制代码

如果函数需要抛出异常,则可不写 catch。
  1. try(InputStream fis = new FileInputStream("e:\\a.txt")) {
  2.     try(OutputStream fos = new FileOutputStream("e:\\b.txt")) {
  3.         byte[] buf = new byte[fis.available()];
  4.         fis.read(buf);
  5.         Arrays.sort(buf);
  6.         fos.write(buf);
  7.     }
  8. }
复制代码


回复 使用道具 举报
我记得以前再其他地方听过……

如果你需要在这里立即处理异常,就使用try/catch,
如果你不想在这里处理,而是丢给其他类处理,就使用throws。
回复 使用道具 举报
阿卜 发表于 2016-7-16 12:29
我记得以前再其他地方听过……

如果你需要在这里立即处理异常,就使用try/catch,

呃……不知道怎么在楼内回- -

不是太懂你想表达的意思- -,帖子不是说是否需要try/catch块么
并且两段代码都有close()并抛出异常啊……
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马