The throws part does not indicate that the method should raise the mentioned exception (s), not even with certain actions. It only reports that the function is allowed for this.
The inclusion of throws UnsupportedOperationException , therefore, does not mean that the method is not supported. In addition, UnsupportedOperationException is a RuntimeException , so the throw method can anyway
Now, for the reason that this is required in the method signature, it all comes down to the possibility of checking exceptions in general. In order for the compiler to decide whether a method can only throw the indicated exceptions, it must be able to decide that the methods it calls cannot throw the thrown exceptions.
This means, for example, that overriding a method means that you cannot add exceptions that could be thrown, otherwise you could violate the ability to verify that the method that calls this method cannot throw anything other than the specified one. Another way would be possible (but I'm not sure if Java supports this) by overriding a method that can throw one that cannot be thrown.
So for example:
class B { int fubar(int) throws ExceptionA { } int frob(int) throws ExceptionA { return fubar(int); } } class D extends B { int fubar(int) throws ExceptionB { } }
Now frob indicated, perhaps throw only ExceptionA , but when calling this.fubar it will open the possibility that something else will be selected, but fubar is determined, perhaps only throw ExceptionA . Therefore, D.fubar is an invalid override, as this will open the possibility that this.fubar actually throws ExceptionB , and the compiler cannot guarantee that frob does not throw ExceptionB .
source share