aufgerufen wird Ich fange direkt mit dem Code an. Dies ruft eine Methode unter Verwendung von ReflexionsRuntimeException-Propagierung von einer Methode, die durch Reflexion
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);
}
und ruft die Methode durch den folgenden Code, der eine PropertiesDontMatchException
(Laufzeit) führt.
...
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.");
}
...
Die Sache ist die, dass meine Laufzeitausnahme in InvocationTargetException
und in den ersten Code-Schnipseln gefangen gewickelt wird. Das ist nicht was ich will. Aber nach der Dokumentation ist es das richtige Verhalten.
So kam ich mit dieser Lösung bis
...
} catch (InvocationTargetException e) {
if (e.getCause() instanceof PropertiesDontMatchException) {
throw (PropertiesDontMatchException) e.getCause();
}
throw new InternalException("Method invocation with reflection failed.", e);
}
...
Ist dies die richtige Art und Weise, wie meine Laufzeitausnahme zu propagieren oder gibt es eine bessere Lösung für dieses Problem?
Ich kann dies nicht tun, weil ich andere Laufzeitausnahmen in InternalException umbrechen möchte, die dann als 500 Interner Fehler an den Client zurückgegeben wird. Die Eigenschaft ..Exception wird als 400 ungültige Anforderung zurückgegeben. Aber danke für die Antwort ... – user219882
Was ist, wenn 'e.getCause() instanceof Error'? – Saintali
@Saintali: Sie haben Recht, ich habe meine Antwort aktualisiert und Guava Tipp hinzugefügt. Vielen Dank! –