newExc.initCause(e); initCause是什么意思?用来干嘛的

来源:1-9 经验总结

情天395

2018-10-31 10:51

public class ChainTest {

 public static void main(String[] args) {
  ChainTest ct = new ChainTest();
  try{
   ct.test2();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public void test1() throws DrunkException{
  throw new DrunkException("喝车别开酒");
 }
 public void test2(){
  try {
   test1();
  } catch (DrunkException e) {
   
   RuntimeException newExc =
    new RuntimeException(e);
   //newExc.initCause(e);
   throw newExc;
  }
 }


写回答 关注

1回答

  • 吃着包子的游不动
    2018-10-31 15:42:00

    test1()函数抛出的是DrunkException异常,test2()函数调用了test1()并对test1()中的异常进行了处理,抛出的是RuntimeException异常,initCase(e)说明了test2()抛出的RuntimeException是由于DrunkException异常引起的。

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409784 学习 · 4339 问题

查看课程

相似问题