코드로 시작합니다. 이것은 하나의 반사리플렉션에 의해 호출 된 메소드의 RuntimeException 전달
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);
}
...
이 내 런타임 예외를 전파 또는이 문제의 더 나은 솔루션이하는 방법을 적절한 방법인가를 내놓았다?
다른 런타임 예외를 InternalException에 랩 한 다음 클라이언트에 500 내부 오류로 반환하기 때문에이 작업을 수행 할 수 없습니다. Properties..Exception은 400 잘못된 요청으로 반환됩니다. 하지만 대답을 주셔서 감사합니다 ... – user219882
만약'e.getCause() instanceof Error'? – Saintali
@Saintali : 당신 말이 맞아요, 내 대답을 업데이 트하고 구아바 팁을 추가했습니다. 감사! –