-3

如果我尝试同时捕获异常并说出这样的 InterruptedException 会发生什么:

try {
  someCode();
} catch (Exception e) {
  doSomething();
} catch (InterruptedException e) {
specialTreatmentForThisException();
}

在这种情况下,InterruptedException 扩展了 Exception。如果抛出 InterruptedException 会发生什么?它会进入那个 catch-clause 和所有其他 Exceptions 的第一个子句吗?

期望的结果是以一种特殊的方式处理一个异常,并以一种方式处理所有其余的异常。

谢谢和最好的问候。

4

2 回答 2

0

异常是父级,因此不使用子级 InterruptedException 类。更改父位置和子位置。

于 2021-06-30T07:59:37.300 回答
0

正如ThomasUser已经添加的那样,您必须首先创建特定异常的 catch 块,最后,您必须使用一般异常。如果您首先使用一般异常,则特定异常将无法访问,编译器将抛出错误提示

错误:异常 InterruptedException 已被捕获 catch(InterruptedException e){

try {
  ---
  ---
}
catch(InterruptedException e) {
  specialTreatmentForThisException();
}
catch(Exception e){
  doStuff();
}
于 2021-06-30T07:04:37.420 回答