I have two snippet of code :
class PreciseRethrow {
public static void main(String[] str) {
try {
foo();
} catch (NumberFormatException ife)
-
The difference is that the compiler can see that the only checked exception that can be rethrown in the first example is a NumberFormatException
. (You are not creating any new exceptions.) Therefore it is happy with the throws NumberFormatException {
declaration in the header.
In the second example however, you are explicitly throwing a checked exception that is not declared in the header, so predictably you get a compiler error.
- 热议问题