小编Saj*_*jhu的帖子

子类catch块是否会捕获已检查的父异常?

我有一个带有父类的已检查异常的方法,它可以抛出父类和子类的异常

public void method() throws ParentException {
    if( false ) throw new ParentException();
    else if( true ) throw new ChildException(); // this one is thrown
}
Run Code Online (Sandbox Code Playgroud)

我有一个级联catch块,它首先有子例外

try {
    method();
} catch (ChildException e) {
    // I get here?
} catch (ParentException e) {
    // or here?

}
Run Code Online (Sandbox Code Playgroud)

哪个块会捕获抛出的异常?由于该方法仅显式声明了ParentException,因此ChildException是否会显示为ParentException的实例?

java checked-exceptions

8
推荐指数
1
解决办法
3517
查看次数

标签 统计

checked-exceptions ×1

java ×1