Will catching an exception catch a parent class of that exception

后端 未结 3 527
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 12:40

In Java I have a method catching an exception \'ChildException\' that extends \'NewException\'. If that method calls another method that throws a \'NewException\' and let\'s

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 13:30

    Think of it as an instanceof test

    e.g.

    if (e instanceof ChildException) {
     ...
    }
    

    so specifying a class type will catch the class and its subclasses.

提交回复
热议问题