Si la valeur de retour de l'API n'est pas valide, je souhaite utiliser une exception différente en fonction de l'API.
Un mémorandum qui semble pouvoir être utilisé comme ça dans une situation où l'essentiel du traitement semble être similaire
ThrowExceptionTest.java
package reflection;
public class ThrowExceptionTest {
public static void main(String[] args) {
try {
throwException("w");
} catch (WwwwException e) {
System.out.println(e.getMessage());
} catch (ZzzzException e) {
System.out.println(e.getMessage());
}
}
/**
*Basculer l'exception levée par l'indicateur.
*
* @param flg
* @throws WwwwException
* @throws ZzzzException
*/
private static void throwException(String flg) throws WwwwException, ZzzzException {
WwwwException w = new WwwwException("w pas bon");
ZzzzException z = new ZzzzException("Non avec z");
if (ApiName.W.getText().equals(flg)) {
throw w;
}
if (ApiName.Z.getText().equals(flg)) {
throw z;
}
}
}
class WwwwException extends Exception {
private static final long serialVersionUID = 1L;
WwwwException(String msg) {
super(msg);
}
}
class ZzzzException extends Exception {
private static final long serialVersionUID = 1L;
ZzzzException(String msg) {
super(msg);
}
}
enum ApiName {
W("w"),
Z("z");
private String text;
private ApiName(final String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Mauvais point: c'est ennuyeux d'avoir une clé Enum
Je voulais vraiment faire quelque chose de bien avec la classe Exception comme argument.
Arguments: Classe <? Étend Throwable>
Traitement: lancer l'argument .newInstance ()
Comme
Mais c'est parce que je dois utiliser la réflexion
Recommended Posts