0

I have a method where I need to return a specific object if found, otherwise throw an exception. So I wrote the following:

public CustomerDetails findCustomer( String givenID ) throws CustomerNotFoundException{
    for(CustomerDetails nextCustomer : customers){
        if(givenID == nextCustomer.getCustomerID()){
            return nextCustomer;
        }else{
            throw new CustomerNotFoundException();
        }
    }
}

But it requires me to add a return statement at the bottom of the method. Is there a way to ignore this?

3
  • 2
    What do you want to happen if customers is empty? And do you really want to throw an exception if the very first customer is not the one you want? (That's what your current code would do... you'll never go into a second iteration of the loop.) Commented Feb 25, 2015 at 13:28
  • The compiler actually complains about the case where the customers collection is empty. In that case, neither the return nor the throw are executed. You should implement it the way TheLostMind suggested below. Commented Feb 25, 2015 at 13:29
  • 1
    Thank you for the comments, only after reading them I realized how stupid the exception was implemented. Commented Feb 25, 2015 at 13:32

6 Answers 6

6

It asks you to provide a valid outcome from the method for the case when the loop is not executed (i.e. customers is empty). You have to do this:

for (CustomerDetails nextCustomer : customers){
    if (givenID == nextCustomer.getCustomerID()){
        return nextCustomer;
    }
}
throw new CustomerNotFoundException();

because otherwise you would throw the exception after the first element that doesn't meet the condition provided in the if.

Sign up to request clarification or add additional context in comments.

Comments

3

Change your code to :

public CustomerDetails findCustomer( String givenID ) throws CustomerNotFoundException{
    for(CustomerDetails nextCustomer : customers){
        if(givenID == nextCustomer.getCustomerID()){
            return nextCustomer;
        }
    }
    throw new CustomerNotFoundException();
}

2 Comments

this is not the same logic as in quesiton. It will loop through every customer if given id not equals
@VishnudevK - Actually, the OPs logic was wrong.. He will throw an exception if the first element doesn't match the expected id.
1

You can return the object if it is found. If it will be not found it throw an exception at end of the loop:

public CustomerDetails findCustomer( String givenID ) throws CustomerNotFoundException{
    for(CustomerDetails nextCustomer : customers){
        if(givenID.equals(nextCustomer.getCustomerID())){
            return nextCustomer;
       }
    }
       throw new CustomerNotFoundException();
}

Note. you compare strings with ==. Here you have to usethe equals method!

2 Comments

this is not the same logic as in quesiton. It will loop through every customer if given id not equals
@VishnudevK but that is the logic what OP is searching for.
0

An exception should be thrown if an unexpected behavior occurs. A failed search is not an exception, but a rarely common cause.

For the reason of good design, you should not throw the exception. Instead you can expand your calling method to test the result for null-iness or similiar.

Comments

0

You can just add a return; at the end of your method. It won't be accessible so it won't cause issues.

You could also use a try catch around your loop. Here is a handy tutorial for that if you wish to follow this route. http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html

Comments

0

If the customer could not be found in the loop, it should throw exception out of the loop. Also you should use ".equals" instead of "==", because "givenID" is an object.

public CustomerDetails findCustomer( String givenID ) throws CustomerNotFoundException {
    for (CustomerDetails nextCustomer : customers) {
        if (givenID.equals(nextCustomer.getCustomerID())){
            return nextCustomer;
        }
    }
    throw new CustomerNotFoundException();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.