本帖最后由 yaoguicheng 于 2010-01-01 10:39:41 编辑

解决方案 »

  1.   

    感觉和计数器的清零一样。呵呵。只是感觉两个try块互不影响。初始值都要为null;。
      

  2.   

      wc = null;//very import
            try{
                try{
                    wc = new WithCleanup(true);
                }catch(AbortedConstruction e) {
                    System.out.println("Caught " + e);
                }finally {
                    System.out.println("In finally 2,preparing to clean up");
                    wc.cleanup();
                }
            }catch(Exception e){
                System.out.println("Caught exception "+ e);
            }
    我们讨论一下这部分:(1) wc = null开始的时候wc引用没有指向任何有效的WithCleanup对象
    (2) 当第一个try块中执行  wc = new WithCleanup(true);的时候,由于构造器内抛出了异常,一次对象并没有构造成功,也就是说new并没有成功的返回一个有效WithCleanup对象的内存地址。因此wc仍然是null
    (3) 最后执行finally块中的wc.cleanup();是一定会抛出java.lang.NullPointerException的运行时异常。接着虚拟机退出,程序再也不继续执行下去了。