I have two snippet of code :
class PreciseRethrow {
public static void main(String[] str) {
try {
foo();
} catch (NumberFormatException ife)
You are attempting to throw an Exception, however you declare that you will throw a NumberFormatException
, which is a subtype of Exception
. All NumberFormatException
s are Exception
s, however not all Exception
s are NumberFormatException
, so it is not allowed. In the first example however, although you are catching an Exception
, the compiler and IDE know that it will only ever be a NumberFormatException
, which is safe.