Consider the following example:
Integer add (Integer a, Integer b) {
try {
return a + b;
} catch (Exception e) {
throw e;
}
}
Of course, the addition of two numbers cannot throw any checked exceptions. However, in Java 6, the compiler sees throw e
, where e
is an Exception
, and concludes that the method can throw any Exception
. This requires add to declare that it throws Exception
.
From Java 7, the compiler is a bit more clever with working out what types of exception e
can be when it is re-thrown. In this case, it is able to work out that e
can only be a RuntimeException
(which is unchecked), and thus the declaration that add throws Exception
is no longer necessary.