A method can only throw a checked exception if it is declared with the throws clause. In this way, a code that uses this method knows that it is possible that the exception is thrown during the call to it.
So, if metodoA
calls metodoB
and metodoB
has, eg throws FileNotFoundException
, the compiler knows there are two options:
-
metodoA
does not address the possible exception. When metodoB
throws the exception, in turn metodoA
will propagate the exception to the method that invokes it. Therefore, because of the above, metodoA
can throw the exception, so you need to declare it in your throws
.
-
The call to metodoB
is within a try/catch
that captures the exception defined in throws
and does not re-launch it. Since there is no risk that metodoA
throw the exception, you do not need to declare it in your throws
clause.