I have an extremely basic function which searches through an ArrayList of CustomerAccount
, and returns the accounts matching the regNum
argument is passed to it. However, as soon as the CustomerAccountNotFoundException is thrown, my for loop breaks.
public CustomerAccount findCustomer(String regNum) throws CustomerNotFoundException
{
CustomerAccount customer = null;
for (int i=0; i < accounts.size(); i++)
{
if(regNum.equals(accounts.get(i).getCustomerVehicle().getRegistration()))
{
customer = accounts.get(i);
}
else
{
throw new CustomerNotFoundException();
}
}
return customer;
}
I've tested this by printing the value of i
after an Exception, which keeps being reset to 0. How can I continue the loop after the Exception is thrown? I want it thrown each time the account doesn't match, and the account returned when it does. I've also tried continue;
which doesn't work.