我看了侯捷写的Java编程思想后,对于'通过异常处理错误‘这一章有一个地方想不通,在原书中的P392的Rethrowing.java与P394的ThrowOut.java使得我对Throwable与Excepiton的关系产生了疑惑,我查了帮助文档说了Throwable是Exception的父类,当P392的Rethrowing.java却使我不明白了,请看Rethowing.java的源代码:   
    
  public   class   Rethowing   
  {   
  public   static   void   f()   throws   Exception   
  {   
  System.out.println("originating   the   exception   in   f()");   
  throw   new   Exception("thrown   from   f()");   
  }   
    
  public   static   void   g()   throws   Throwable   
  {   
  try   
  {   
  f();   
  }   
  catch(Exception   e)   
  {   
  System.err.println(   
                                                              "Inside   g(),e.printStackTrace()");   
  e.printStackTrace();   
  //throw   e;     //(1)   
  throw   e.fillInStackTrace();     //(2)   
  }   
    
  }   
    
  public   static   void   main(String[]   args)   throws   Throwable   
  {   
  try   
  {   
  g();   
  }   
  catch(Exception   e)   
  {   
  System.err.println(   
                                                                "Caught   in   main,e.printStackTrace()");   
  e.printStackTrace();   
  }   
  }   
  }   
    
  当将(1)注释掉结果为:   
  originating   the   exception   in   f()   
  Inside   g(),e.printStackTrace()   
  java.lang.Exception:   thrown   from   f()   
                  at   Rethowing.f(Rethowing.java:6)   
                  at   Rethowing.g(Rethowing.java:13)   
                  at   Rethowing.main(Rethowing.java:29)   
  Caught   in   main,e.printStackTrace()   
  java.lang.Exception:   thrown   from   f()   
                  at   Rethowing.g(Rethowing.java:20)   
                  at   Rethowing.main(Rethowing.java:29)   
    
  当将(2)注释掉结果为:   
  originating   the   exception   in   f()   
  Inside   g(),e.printStackTrace()   
  java.lang.Exception:   thrown   from   f()   
                  at   Rethowing.f(Rethowing.java:6)   
                  at   Rethowing.g(Rethowing.java:13)   
                  at   Rethowing.main(Rethowing.java:29)   
  Caught   in   main,e.printStackTrace()   
  java.lang.Exception:   thrown   from   f()   
                  at   Rethowing.f(Rethowing.java:6)   
                  at   Rethowing.g(Rethowing.java:13)   
                  at   Rethowing.main(Rethowing.java:29)   
    
  疑惑:   
  1。当将(1)注释掉时,调用throw   e.fillInStackTrace();时,将抛出一个Throwable型的引用这可以理解,但当这个Throwable引用抛出到main()函数时,却能与Exception匹配,运行catch(Exception   e)中的语句!!!!   
  Throwable不是Exception的父类吗???为什么从父类转型到子类不需要强制转换,就可以匹配成功???   
  当我硬着头皮往下看是,更加迷糊了,请见P394的ThrowOut.java源代码:   
    
  public   class   ThrowOut   
  {   
  public   static   void     main(String[]   args)   throws   Throwable   
  {   
  try   
  {   
  throw   new   Throwable();   
  }   
  catch(Exception   e)   
  {   
  System.out.println("caught   in   main()");   
  }   
  }   
  }   
    
  在ThrowOut.java中,抛出一个Throwable()去不会与Exception匹配,这不是跟上面的情况矛盾吗???   
    
  2。在Rethowing.java中的f()与g()成员函数中都调用了e.printStackTarce(),为什么两次的输出结果都一样,难道Java这么有预见性,还没将异常抛到上层就可以知道其行动路线???   这是原帖
http://topic.csdn.net/t/20030403/11/1612923.html

解决方案 »

  1.   

    第一个问题暂时没有答案
    2:e.printStackTarce()显示的是原始异常抛出的地方的信息,不管抛多少层都是,所以两次输出的结果是一样的,
     调用e.fillInStackTrace()后,把g()作为异常抛出点了,就不是f()了,所以会少打印一个f()的信息。
    希望有人解答第一个。
      

  2.   

    2:f()里面没有调用e.printStackTarce(),只有g()和main()里调用了啊!
      

  3.   

    第二个问题 问题意思理解错了吧
    他的意思是在第一个g()中没必要抛到Main中来? 是这个意思吗?
      

  4.   

    那是第一个问题吧,他理解错了吧,f()中并没有调用e.printStackTarce()啊,参看五楼回复。
      

  5.   

    第二个问题:
     他的意思是不是:把(2)注释掉之后,输出结果应该是:
     originating  the  exception  in  f()  
      Inside  g(),e.printStackTrace()  
      java.lang.Exception:  thrown  from  f()  
                      at  Rethowing.f(Rethowing.java:6)  
                      at  Rethowing.g(Rethowing.java:13)  
                      at  Rethowing.main(Rethowing.java:29) 
      Caught  in  main,e.printStackTrace()  
      java.lang.Exception:  thrown  from  f()  
                      at  Rethowing.f(Rethowing.java:6)  
                      at  Rethowing.g(Rethowing.java:13)  
                      at  Rethowing.main(Rethowing.java:29)  
    红色部分是不是应该去掉?
      

  6.   

    把(2)注释掉后,两次打印的错误消息应该是一样的。把(1)注释掉后,第二次(main里面的打印)会少f()
    的调用,因为把g()当作异常抛出点了。
      

  7.   


    public static main(String[] sdf){
        System.out.println("Hello World!");
    }
      

  8.   

    Throwable不是Exception的父类吗???为什么从父类转型到子类不需要强制转换,就可以匹配成功???
      

  9.   

    Java编程思想  是侯捷写的吗?
      

  10.   


    public static void g() throws Throwable   {
       // ...
    }public static void main(String[] args) throws Throwable   
      {   
      try   
      {   
      g();   
      }   
      catch(Exception e)   
      {   
      System.err.println(   
      "Caught in main,e.printStackTrace()");   
      e.printStackTrace();   
      }   
      }   
      }   
    这里根本编译都通过不了。