Java数组搜索,基于i';数组内的snt异常错误

Java数组搜索,基于i';数组内的snt异常错误,java,arrays,exception-handling,arraylist,crash-reports,Java,Arrays,Exception Handling,Arraylist,Crash Reports,我有一个程序,用户在一年内输入,然后搜索一个包含年数的数组 当用户进入“无效”年份时,程序崩溃,我如何克服这一点?我需要使用try-and-catch语句吗?是。例如,如果您试图“捕获”一个InvalidYearException,您将执行以下操作: try { // Your code (the code that will possibly cause an exception to be thrown) } catch(InvalidYearException exception

我有一个程序,用户在一年内输入,然后搜索一个包含年数的数组


当用户进入“无效”年份时,程序崩溃,我如何克服这一点?我需要使用try-and-catch语句吗?

是。例如,如果您试图“捕获”一个
InvalidYearException
,您将执行以下操作:

try {
    //  Your code (the code that will possibly cause an exception to be thrown)
} catch(InvalidYearException exception) {
    //  Your code (the code that will be executed when you 'catch' an InvalidYearException)
    //  for example:
    System.out.println("Error!" + exception.getMessage());
}

是的,请使用try-catch,因为这是一个自定义错误条件,而不是内置的。这是一个笑话吗?如果没有,我认为问题出在第42行的
If
条件下。向我们显示您的代码,否则我们无法帮助您。@Abu谢谢您再次检查@我不想在这里放代码,因为我想学习自己!谢谢你,安维