I have a program where a user enters in a year and it searches an array with years inside of it.
When a user enters a 'invalid' year the program crashes, how can i over come this? Will i need to use a try and catch statement?
Yes. For example, if you were trying to 'catch' an InvalidYearException
, you would do something like this:
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());
}