如果在我的菜单数组中按名称找到,我有这个方法返回一个食谱对象。如果它没有找到它,它会抛出一个自定义的 RecipeNotFoundException 并只是说它没有找到......
public static Recipe getRecipe(String recipeName){
try{
for(int j = 0 ; j < Menu.length ; j++){
if(Menu[j].getName().equals(recipeName)){
return (Recipe)Menu[j];
}
else{
throw new RecipeNotFoundException();
}
}
}catch(RecipeNotFoundException e){
System.out.println("Recipe Not Found!");
}
return null;
}
在当前条件下,它只检查第一个菜单点,我如何让它在抛出异常之前检查所有这些点!?我尝试过反转和翻转,但它只会导致 NullPointerExceptions 并检查整个事情而无法进行投掷。有人得到一些指导吗?提前致谢!