私はコードから始めます。これはリフレクションを使用してメソッドを呼び出します
try {
Method method = states.getClass().getDeclaredMethod(
getCurrentStateId() + "_" + request.getEvent());
states.setData(request, dataManager);
method.invoke(states);
} catch (NoSuchMethodException e) {
logger.debug("Method " + getCurrentStateId() + "_" + request.getEvent()
+ " cannot be found - invocation not performed.", e);
} catch (IllegalArgumentException e) {
throw new InternalException("Method invocation with reflection failed.", e);
} catch (IllegalAccessException e) {
throw new InternalException("Method invocation with reflection failed.", e);
} catch (InvocationTargetException e) {
throw new InternalException("Method invocation with reflection failed.", e);
}
PropertiesDontMatchException
そして、 (ランタイム)をスローする次のコードでメソッドを呼び出します。
...
if (totalCredits < minimumCredits || totalCredits > maximumCredits) {
throw new PropertiesDontMatchException("Minimum amount of credits=" + minimumCredits
+ ", maximum amount of credits=" + maximumCredits + ". Your amount of credits=" + totalCredits + ". You have to modify your set of subjects.");
}
...
問題は、私のランタイム例外がInvocationTargetException
最初のコードスニペットにラップされてキャッチされていることです。これは私が望むものではありません。しかし、ドキュメントによると、それは正しい動作です。
だから私はこの解決策を思いついた
...
} catch (InvocationTargetException e) {
if (e.getCause() instanceof PropertiesDontMatchException) {
throw (PropertiesDontMatchException) e.getCause();
}
throw new InternalException("Method invocation with reflection failed.", e);
}
...
これは私のランタイム例外を伝播する適切な方法ですか、それともこの問題のより良い解決策はありますか?