Final edit due to this question having been marked as duplicate: this question is about the semantics of the throws
declaration - the question which is this said to be a duplicate of handles different aspects of throws
and none of those 15 answers there gave me the insight of the chosen answer here. Anyway - so let's keep it here as a duplicate.
In Java you have to declare a throws
clause and name the exceptions that this method could throw - so the easy way is to just declare
void myMethod(...) throws Exception
or you could be more specific and for example state
void myMethod(...) throws SQLException, NamingException
in case the method may just throw these two.
I understand that it makes a difference whether in a try/catch block I
try { ... } catch (Exception exc) { ... }
or
try { ... } catch (SQLException | NamingException exc) { ... }
because the first will also catch a RuntimeException
, the second won't - so this is a difference.
But is there also any difference in declaring throws Exception
vs. throws SQLException, NamingException
concerning the semantics of the program? The second may be more readable, but I don't see any other difference. Is there any?