此方法来自于Throwable:
    public void printStackTrace() {
        printStackTrace(System.err);
    }那么在下边这个程序里:
class A 

      int chufa(int a,int b)throws myexception 
      { 
              if(b>50) 
                    throw new myexception("除数不能大于50"); 
              return a/b; 
      } 
} class myexception extends Exception 

      myexception(String str) 
      { 
          super(str); 
      } 
} class Text 

      public static void main (String[] args) 
      { 
              A a=new A(); 
              try 
              {                                
                  a.chufa(1,51); 
              } 
              catch(myexception ex) 
              { 
                   ex.printStackTrace();  //这句如果注释掉一点都没有了。
              } 
    } 
} 输出为:
myexception: 除数不能大于50             //这个它是怎么调用的。糊里糊涂就输出了。
    at A.chufa(yu.java:6)
    at Text.main(yu.java:26)

解决方案 »

  1.   

    printStackTrace() 
              将此 throwable 及其追踪输出至标准错误流。
    a.chufa(1,51); 你在这里有这个方法的实现.
    int chufa(int a,int b)throws myexception 
          { 
                  if(b>50) 
                        throw new myexception("除数不能大于50"); 
                  return a/b; 
          } 
    在这里已经try到了. 就是说除数已经大于50了..就应该要抛出异常了..
    这句只是打印了一下.
      

  2.   

    printStackTrace() 
              Prints this throwable and its backtrace to the standard error stream.This method, printStackTrace(), is inherited from class Throwable, which is the superclass of all errors and exceptions in the Java language.So if you write a subclass 'myexception' and do not rewrite printStackTrace(), it will just be used as what was defined in Throwable by inheritance.
      

  3.   

    printStackTrace()
    将此 throwable 及其追踪输出至标准错误流。此方法将此 Throwable 对象的堆栈跟踪输出至错误输出流,作为字段 System.err 的值。输出的第一行包含此对象的 toString() 方法的结果。剩余行表示以前由方法 fillInStackTrace() 记录的数据。
    由于System.err是输出流,所以会打印出下面那么信息
    myexception: 除数不能大于50            
        at A.chufa(yu.java:6) 
        at Text.main(yu.java:26) 
    又输出的第一条应该是打印myexception.toString();但myexception.toString()也是空的,所以应该是打印了其父类Exception("除数不能大于50")的toString.具体为什么会打印其父类的toString();这个我自己也弄不明白 至于你说的把那句话注释了,就没打印结果。这是由于你捕捉到了异常,而处理块中做没有任何处理,那肯定没输出了
      

  4.   

    a.chufa(1,51)-->throw new myexception("除数不能大于50")-->myexception(String str)-->super(str)-->Exception(String message)这里str=message="除数不能大于50"
      

  5.   

    出异常时,调用了这个构造函数啊 
    myexception(String str) 
          { 
              super(str); 
          }