3

First of all, I want to make something clear before I get yelled at: I have no plans to write any code that will ever use this sort of control flow; its obviously terrible. Forget about a use case, this is more a question about whether its possible or not:

I want to know if its possible to know if somewhere up the call stack, a function is being called within a catch block for a particular exception.

Let's say I have a function:

foo() {
try {
    something
}
catch (SuperException s) {
    bar()
}

So given NO control over the method signature of bar, and given bar is also called by many other functions, can bar behave differently if called in catch block catching a superexception? (and no, being called by foo isn't a sufficient condition, its the super exception that matters)

4

1 に答える 1

6

コールスタックを確認して、各コールの行番号を取得できます。

バイトコードを読み取って、catchブロックがどこにあり、どのコード行がそれらのブロックにあるかを判断できます。

2つを一致させて、コールスタックのどこかがキャッチブロック内にあるかどうかを確認します。

注:これは、コードにデバッグ情報がコンパイルされている場合にのみ機能します。

于 2012-06-14T15:50:59.373 に答える