I would like to handle an exception but the Eclipse editor is giving me tips which I'm not understanding. This is the method's code:
public Collection<String> listOfFriends(String codePerson)
throws NoSuchCodeException {
Collection<String> s = new LinkedList<>();
try {
people.get(codePerson).getFriendsCodes().forEach( code -> s.add(getPerson(code)));
} catch (Exception e) {
e.printStackTrace();
}
return s;
Where getPerson is:
public String getPerson(String code) throws NoSuchCodeException {
if(!people.containsKey(code)){
throw new NoSuchCodeException();
}
return people.get(code).getCode() + " " + people.get(code).getName() + " " + people.get(code).getSurname();
and the exception is:
public class NoSuchCodeException extends Exception {
NoSuchCodeException(){}
NoSuchCodeException(String msg){
super(msg);
}
}
Eclipse returns two errors in the listOfFriends
method, one where getPerson()
is invoked, saying "Unhandled exception type NoSuchCodeException" and the other one in the catch row, saying "Unreacheable catch block for NoSuchCodeException. This exception is never thrown from the try statement body".
PS: I added try and catch in the listOfFriends
method just to try to solve the problem, since, as far as I know, given that the method has in its declaration "throws NoSuchCodeException", it shouldn't be obligatory, right?
Thanks in advance.