Why throws
, on a method, is part of its signature? It seems strange to include it. Here is an example where it is in the way:
@Overide
public void foo() {
throw new UnsupportedOperationException();
}
If anyone were to see this method from the outside, they might try to use it without knowing that it is not supported. They would only learn it on trying to run the code.
However, if they could do something like this they would know by looking at the method that it is not supported and if UnsupportedOperationException
was not extending RuntimeException
, they would get a compilation error. EDIT1: But this is not possible because throws
is part of the signature so override will not work.
@Overide
public void foo() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
This question concerns Java's design, so I know that it might be hard to answer without one of the people that work on it drops by and answers it, but I was hoping that maybe this question has been asked to them before or that there might be an obvious reason to have it this way to explain why.
@Override
which indicates that it should follow the contract (abstract or interface) but for unchecked exceptions you are not obligated to do so. Whether you chose to throw it or not always follow the good pratice as @danstahr mentioned, add it on the javadoc. – FenlandNullPointerException
orArrayIndexOutOfBoundsException
. I would not want to include them in each and every method signature. – Mayle