Java Exception学习

参考:try、catch、finally、return的执行顺序    http://t240178168.iteye.com/blog/1660414


 

通过对Java Core的学习,对java exception有了新的认识,之前在处理异常的时候,会采用try...catch...finally这种形式:

Java代码   收藏代码
  1. public void connect2() {  
  2.         Socket client   = null;  
  3.         PrintWriter out = null;  
  4.         Scanner scanner = null;  
  5.         try {  
  6.             client = new Socket("173.39.171.51"12211);  
  7.             client.setSoTimeout(5000);  
  8.             out     = new PrintWriter(client.getOutputStream());  
  9.             scanner = new Scanner(client.getInputStream());  
  10.             while (scanner.hasNextLine()) {  
  11.                 System.out.println("Recive :" + scanner.nextLine());  
  12.                 out.println("Hello.");  
  13.                 TimeUnit.SECONDS.sleep(5);  
  14.             }  
  15.         } catch (IOException | InterruptedException e) {  
  16.             //TODO your biz  
  17.             e.printStackTrace();  
  18.         }finally{  
  19.             if(null != out)  
  20.                 out.close();  
  21.             if(null != scanner)  
  22.                 scanner.close();  
  23.             if(null != client)  
  24.                 try {  
  25.                     client.close();  
  26.                 } catch (IOException e) {  
  27.                     e.printStackTrace();  
  28.                 }  
  29.         }  
  30.     }  

 

看了Java Core上面的讲解后,新的异常处理会采用下面的形式:

Java代码   收藏代码
  1. public void connect() {  
  2.         try {  
  3.             Socket client   = null;  
  4.             PrintWriter out = null;  
  5.             Scanner scanner = null;  
  6.             try {  
  7.                 client = new Socket();  
  8.                 client.connect(new InetSocketAddress("173.39.171.51"12211),5000);  
  9.                 out     = new PrintWriter(client.getOutputStream(), true);  
  10.                 scanner = new Scanner(client.getInputStream());  
  11.                 while (scanner.hasNextLine()) {  
  12.                     System.out.println("Recive :" + scanner.nextLine());  
  13.                     out.println("Hello.");  
  14.                     TimeUnit.SECONDS.sleep(5);  
  15.                 }  
  16.             } finally {  
  17.                 if(null != out)  
  18.                     out.close();  
  19.                 if(null != scanner)  
  20.                     scanner.close();  
  21.                 if(null != client)  
  22.                     client.close();  
  23.             }  
  24.         } catch (IOException | InterruptedException e) {  
  25.             //TODO your biz  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  

 

第二中种形式的代码风格看着更加简洁,职能也更加专一:里面的try...finally 处理资源关闭;外面的try...catch处理异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值