In previous versions of java, rethrowing an exception was treated as throwing the type of the catch parameter.
For example:
public static void test() throws Exception{
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
df.parse("x20110731");
new FileReader("file.txt").read();
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
throw e;
}
}
In Java 7, you can be more precise about the exception being thrown, if you declare the exception final
:
//(doesn't compile in Java<7)
public static void test2() throws ParseException, IOException{
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
df.parse("x20110731");
new FileReader("file.txt").read();
} catch (final Exception e) {
System.out.println("Caught exception: " + e.getMessage());
throw e;
}
}
My question: The docs say that I need to declare the Exception final
. But if I don't, the code above still compiles and works. Am I missing something?
References:
Project Coin: multi-catch and final rethrow
Add more flexible checking for rethrown exceptions