Suppose I have this class:
public abstract class GraphEdge implements Comparable {
public abstract double getLength() throws DependencyFailureException;
@Override
public int compareTo(Object obj) {
return Double.compare(getLength(), ((GraphEdge)obj).getLength());
}
}
Don't worry about checking the obj type in compareTo for now. getLength () throws a DependencyFailureException if its dependency does not work. Because getLength () throws an exception, compareTo gives a compile-time error because the DependencyFailureException is not thrown.
I donโt know if try / catch can do this best, as if an exception occurred in getLength (), which means that the length of this edge no longer makes sense and comparing it with another double does not help. I think if the exception came from getLength (), it should just get the surface at the top of the hirachey call.
DependencyFailureException is a custom exception that I can change if necessary.
What to do to match GraphEdge?
source
share