I am getting the following code snippet from Android's Pair.java
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
final Pair<F, S> other;
try {
other = (Pair<F, S>) o;
} catch (ClassCastException e) {
return false;
}
return first.equals(other.first) && second.equals(other.second);
}
I was wondering, how is it possible to have ClassCastException, after instanceof returns true.
ClassCastException, are you? It should be impossible in this case. Just because you found code somewhere that catches some random exception doesn't mean this exception can actually be thrown.