java 7中再谈try catch

java 7中的try catch除了之前谈到的新特性外,本文简单来例子小结下,其实还是有不少地方要注意的,首先看一个典型的代码:

先来两个异常类:

 
Java代码 复制代码
  1. public class ExceptionA extends Exception{   
  2.     public ExceptionA(String message){   
  3.         super(message);   
  4.     }   
  5. }   
  6. public class ExceptionB extends Exception{   
  7.     public ExceptionB(String message){   
  8.         super(message);   
  9.     }   
  10. }  
public class ExceptionA extends Exception{
    public ExceptionA(String message){
        super(message);
    }
}
public class ExceptionB extends Exception{
    public ExceptionB(String message){
        super(message);
    }
}



再创建一个资源类oldresource,如下:
Java代码 复制代码
  1.   
  2. public class OldResource{   
  3.     public void doSomeWork(String work) throws ExceptionA{   
  4.         System.out.println("Doing: "+work);   
  5.         throw new ExceptionA("Exception occured while doing work");   
  6.     }   
  7.     public void close() throws ExceptionB{   
  8.         System.out.println("Closing the resource");   
  9.         throw new ExceptionB("Exception occured while closing");   
  10.     }   
  11. }  
public class OldResource{
    public void doSomeWork(String work) throws ExceptionA{
        System.out.println("Doing: "+work);
        throw new ExceptionA("Exception occured while doing work");
    }
    public void close() throws ExceptionB{
        System.out.println("Closing the resource");
        throw new ExceptionB("Exception occured while closing");
    }
}




我们开始使用之:
Java代码 复制代码
  1. public class OldTry {   
  2.     public static void main(String[] args) {   
  3.         OldResource res = null;   
  4.         try {   
  5.             res = new OldResource();   
  6.             res.doSomeWork("Writing an article");   
  7.         } catch (Exception e) {   
  8.             System.out.println("Exception Message: "+   
  9.                       e.getMessage()+" Exception Type: "+e.getClass().getName());   
  10.         } finally{   
  11.             try {   
  12.                 res.close();   
  13.             } catch (Exception e) {   
  14.                 System.out.println("Exception Message: "+   
  15.                          e.getMessage()+" Exception Type: "+e.getClass().getName());   
  16.             }   
  17.         }   
  18.     }   
  19. }  
public class OldTry {
    public static void main(String[] args) {
        OldResource res = null;
        try {
            res = new OldResource();
            res.doSomeWork("Writing an article");
        } catch (Exception e) {
            System.out.println("Exception Message: "+
                      e.getMessage()+" Exception Type: "+e.getClass().getName());
        } finally{
            try {
                res.close();
            } catch (Exception e) {
                System.out.println("Exception Message: "+
                         e.getMessage()+" Exception Type: "+e.getClass().getName());
            }
        }
    }
}




看输出:
   Doing: Writing an article
Exception Message: Exception occured while doing work Exception Type: javaapplication4.ExceptionA
Closing the resource
Exception Message: Exception occured while closing Exception Type: javaapplication4.ExceptionB

再来看java 7中的新写法,代码如下:
Java代码 复制代码
  1. public class NewResource implements AutoCloseable{   
  2.     String closingMessage;   
  3.     
  4.     public NewResource(String closingMessage) {   
  5.         this.closingMessage = closingMessage;   
  6.     }   
  7.     
  8.     public void doSomeWork(String work) throws ExceptionA{   
  9.         System.out.println(work);   
  10.         throw new ExceptionA("Exception thrown while doing some work");   
  11.     }   
  12.     public void close() throws ExceptionB{   
  13.         System.out.println(closingMessage);   
  14.         throw new ExceptionB("Exception thrown while closing");   
  15.     }   
  16.     
  17.     public void doSomeWork(NewResource res) throws ExceptionA{   
  18.         res.doSomeWork("Wow res getting res to do work");   
  19.     }   
  20. }  
public class NewResource implements AutoCloseable{
    String closingMessage;
 
    public NewResource(String closingMessage) {
        this.closingMessage = closingMessage;
    }
 
    public void doSomeWork(String work) throws ExceptionA{
        System.out.println(work);
        throw new ExceptionA("Exception thrown while doing some work");
    }
    public void close() throws ExceptionB{
        System.out.println(closingMessage);
        throw new ExceptionB("Exception thrown while closing");
    }
 
    public void doSomeWork(NewResource res) throws ExceptionA{
        res.doSomeWork("Wow res getting res to do work");
    }
}





  在JAVA 7中,要自动在try catch中利用到新特性,想不写那么多东西就关闭资源,则可以编写实现 AutoCloseable类的,则都可以利用该特性了。
在主程序中调用:
 
Java代码 复制代码
  1. public class TryWithRes {   
  2.     public static void main(String[] args) {   
  3.         try(NewResource res = new NewResource("Res1 closing")){   
  4.             res.doSomeWork("Listening to podcast");   
  5.         } catch(Exception e){   
  6.             System.out.println("Exception: "+   
  7.              e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());   
  8.         }   
  9.     }   
  10. }  
public class TryWithRes {
    public static void main(String[] args) {
        try(NewResource res = new NewResource("Res1 closing")){
            res.doSomeWork("Listening to podcast");
        } catch(Exception e){
            System.out.println("Exception: "+
		     e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
        }
    }
}




输出结果为:
  Listening to podcast
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA

大家可以思考下为什么这样输出,在新特性中,资源的自动关闭调用了close(),而
NewResource res = new NewResource("Res1 closing")){
已经为closingMessage赋值了,而最后的Exception e是输出了,suprred掉了
exception a和exception b的输出。

再看一个多层嵌套的try catch例子
 
Java代码 复制代码
  1. public class TryWithRes {   
  2.     public static void main(String[] args) {   
  3.         try(NewResource res = new NewResource("Res1 closing");   
  4.             NewResource res2 = new NewResource("Res2 closing")){   
  5.             try(NewResource nestedRes = new NewResource("Nestedres closing")){   
  6.                 nestedRes.doSomeWork(res2);   
  7.             }   
  8.         } catch(Exception e){   
  9.             System.out.println("Exception: "+   
  10.             e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());   
  11.         }   
  12.     
  13.     }   
  14. }  
public class TryWithRes {
    public static void main(String[] args) {
        try(NewResource res = new NewResource("Res1 closing");
            NewResource res2 = new NewResource("Res2 closing")){
            try(NewResource nestedRes = new NewResource("Nestedres closing")){
                nestedRes.doSomeWork(res2);
            }
        } catch(Exception e){
            System.out.println("Exception: "+
		    e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
        }
 
    }
}




输出:
  Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA

可以看到,后声明的资源上被最先CLOSE掉的,这里各自原先的exception都被supressed掉了。还可以用e.getSuppressed() 把屏蔽掉的exception都放来,比如
Java代码 复制代码
  1. public class TryWithRes {   
  2.     public static void main(String[] args) {   
  3.         try(NewResource res = new NewResource("Res1 closing");   
  4.             NewResource res2 = new NewResource("Res2 closing")){   
  5.             try(NewResource nestedRes = new NewResource("Nestedres closing")){   
  6.                 nestedRes.doSomeWork(res2);   
  7.             }   
  8.         } catch(Exception e){   
  9.             System.out.println("Exception: "+   
  10.             e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());   
  11.             if (e.getSuppressed() != null){   
  12.                 for (Throwable t : e.getSuppressed()){   
  13.                     System.out.println(t.getMessage()+   
  14.                                " Class: "+t.getClass().getSimpleName());   
  15.                 }   
  16.             }   
  17.         }   
  18.     
  19.     }   
  20. }  
public class TryWithRes {
    public static void main(String[] args) {
        try(NewResource res = new NewResource("Res1 closing");
            NewResource res2 = new NewResource("Res2 closing")){
            try(NewResource nestedRes = new NewResource("Nestedres closing")){
                nestedRes.doSomeWork(res2);
            }
        } catch(Exception e){
            System.out.println("Exception: "+
		    e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
            if (e.getSuppressed() != null){
                for (Throwable t : e.getSuppressed()){
                    System.out.println(t.getMessage()+
                               " Class: "+t.getClass().getSimpleName());
                }
            }
        }
 
    }
}




输出显示:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值