I have created my own type of exception and want to implement it in a method. As of now I have written it in the following way, and it works.
public Worker remov
1 Your code would be more efficient if you implemented Comparable and the compareTo
method in Worker as follows -
@Override
public int compareTo(Object obj) {
// identity.
if (obj == this) {
return 0;
} else if (obj instanceof Worker) {
Worker w = (Worker) obj;
if (w.getNumber().compareTo(number) != 0) {
return w.getNumber().compareTo(number);
} else if (w.getLastName().compareTo(lastName) != 0) {
return w.getLastName().compareTo(lastName);
}
return w.getFirstName().compareTo(firstName);
}
return -1;
}
And then use the SortedSet collection type (e.g. TreeSet
), especially the remove method.
2 You should probably just return null. Throwing an Exception
is certainly your choice, but (IMO) an unchecked Exception
should be reserved for unrecoverable errors.