调用方法后的Java反射方法未引发异常
因此,我创建了一个调用方法后的Java反射方法未引发异常,java,debugging,Java,Debugging,因此,我创建了一个invalidInputExceptionexception类,在setter方法中使用该类在没有有效输入时抛出异常 public class kalsi.ContestantInformation { public void setFirstName(String firstName) throws InvalidInputExeption { checkInput(firstName, "Please only enter letter in th
invalidInputException
exception类,在setter方法中使用该类在没有有效输入时抛出异常
public class kalsi.ContestantInformation {
public void setFirstName(String firstName) throws InvalidInputExeption {
checkInput(firstName, "Please only enter letter in the first name");
this.firstName = firstName.replaceAll("\\s", "").toLowerCase();
}
public void setLastName(String lastName) throws InvalidInputExeption {
checkInput(lastName, "Please only enter letter in the last name");
this.lastName = lastName.replaceAll("\\s", "").toLowerCase();
}
public void setStreetNumber(String streetNumber) throws InvalidInputExeption {
checkInput(streetNumber, "Please only enter numbers in the street number");
this.streetNumber = streetNumber.replaceAll("\\s", "").toLowerCase();
}
public void setStreetName(String streetName) throws InvalidInputExeption {
checkInput(streetName, "Please only enter letter in the street name name");
this.streetName = streetName.replaceAll("\\s", "").toLowerCase();
}
public void checkInput(String s, String message) throws InvalidInputExeption {
char[] array = s.toCharArray();
for (char c : array) {
if (!Character.isLetter(c)) {
throw new InvalidInputExeption(message);
}
}
}
public void checkInput(int i, String message) throws InvalidInputExeption {
String s = i + "";
char[] array = s.toCharArray();
for (char c : array) {
if (!Character.isDigit(c)) {
throw new InvalidInputExeption(message);
}
}
}
}
这里是我使用反射调用setFirstName
的主要方法。
注意setFirstName
不是唯一的setter方法。所有其他方法都位于字符串数组中,这些字符串的名称为setMethods
public static void main(String[] args)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, ClassNotFoundException, InstantiationException {
ContestantInformation contestant1 = new ContestantInformation();
String[] questions = { "What is your first name", "What is your last name", "What is your Street Name",
"What is your Sreet Number" };
String[] methods = { "setFirstName", "setLastName", "setStreetName", "setStreetNumber" };
for (int i = 0; i < methods.length; i++) {
do {
try {
flag = false;
System.out.println(questions[i]);
String scannerInput = scanner.nextLine();
classContestantInfo.getDeclaredMethod(methods[i], stringParameter).invoke(contestant1,
scannerInput);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof InvalidInputExeption) {
System.out.println(e.getMessage());
}
}
} while (flag);
}
}
您显示的是
调用TargetException
的消息,而不是InvalidInputException
的消息
您还希望通过将repeat标志设置为true
来确保问题是重复的
将catch子句主体更改为:
if (e.getCause() instanceof InvalidInputExeption) {
System.out.println(e.getCause().getMessage()); // <-- note: getCause() here
flag = true; // Make sure that the question is repeated
}
if(例如,getCause()invalidInputExption实例){
System.out.println(例如getCause().getMessage());//除了这里发布的另一个答案之外,在得到充分回答之前,您不继续循环并问同一个问题的原因似乎是您在成功调用将检查输入的方法之前设置了flag=false
。此外,您应该在be处重置flag=true
对for
循环进行轧棉,这样,如果第二个或后续问题的输入无效,您将继续循环该问题,直到问题得到回答。请正确设置代码格式并提供MCVE。没有足够的(可靠的)问题中的信息用于正确诊断。添加代码以将repeat标志设置为true。我建议将其名称更改为inputeror
或repeatFlag
或更能说明此标志功能的内容。
if (e.getCause() instanceof InvalidInputExeption) {
System.out.println(e.getCause().getMessage()); // <-- note: getCause() here
flag = true; // Make sure that the question is repeated
}