开发者

basic Java question: throwing an exception to a later catch clause?

If you have:

catch (FooException ex) {
    throw new BarException (ex);
}
catch (BarException ex) {
    System.out.println("hi");
}

...and the first catch clause is triggered (i.e. FooExcepetion has occurred), does the new BarException get immediately caught by the subsequent "catch" clause?

O开发者_Go百科r is the new BarException thrown one level up the continuation stack?

I realize this is a basic question. : )


It does not get caught by the 2nd catch clause, no.

Each catch clause in the list is tried, to see if it matches. The first one that matches is the only one that runs, then the code moves on to the finally clause.

Another result of this is that if you have:

try {
    throw SubTypeOfException(...);
} catch(Exception e) {
    ... block 1 ...
} catch(SubTypeOfException e) {
    ... block 2 ...
}

then block 1 is the only one that will run, even though block 2 would have matched. Only the first matching catch clause is evaluated.


It's thrown one level up.

You'll need another try//catch block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜