Research following method:
static private void foo() {
try {
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
This code compiles good despite last catch block actually unreachable.
Now lets comment throw new FileNotFoundException();
row
execute:
OOOPs! we see
Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body
Strange. Why does java use double standards for these situatons?
update for @Peter Rader
static private void foo(FileNotFoundException f) {
try {
throw f;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
work as well as with constructor invocation
update
I noticed that on different versions of java compiler I see different result of compiling this code.
public class RethowTest {
public static void main(String[] args) {
try {
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
throw e;
}
}
}
on my local pc: java 1.7.0_45 -
C:\Program Files\Java\jdk1.7.0_45\bin>javac D:\DNN-Project\DNN-Project\src\main\java\exceptionsAndAssertions\RethowTest.java
D:\DNN-Project\DNN-Project\src\main\java\exceptionsAndAssertions\RethowTest.java:15: warning: unreachable catch clause
} catch (IOException e) {
^
thrown type FileNotFoundException has already been caught
1 warning
java 1.6.0_38
D:\DNN-Project\DNN-Project\src\main\java\exceptionsAndAssertions\RethowTest.java:16: unreported exception java.io.IOException; must be caught or declared to be thrown
throw e;
^
1 error
http://www.compileonline.com/compile_java_online.php (Javac 1.7.0_09) -
HelloWorld.java:9: warning: unreachable catch clause
} catch (IOException e) {
^
thrown type FileNotFoundException has already been caught
1 warning
if(flase){dead code}
is allowed whenwhile(false){dead code}
is not - it can help in simple debugging. – Dodgsonif(flase){dead code}
can help in simple debugging (for instance performed by novice programmers) so they could writeif (false){..} if(true){..} if(false){..} if(true){..}
and by change somefalse
totrue
they could test few combinations of some scenarios. Butwhile(false)
is not that useful because even if we changefalse
totrue
we will end up in infinite loop, so we will not to be able to leave and test other combinations. I am not the author of this idea but it seems reasonable. – Dodgsonstatic final boolean DEBUG = false;
and then write code such as:if (DEBUG) { x=3; }
. So maybe for similar reasons, we compiler allows adding code for supertype exceptions even if all exceptions ware handled (for example to docatch(Exception e){//some kind of logging when we are sure exceptions shouldn't happen in tested case}
). – Dodgsonif(false)
vswhile(false)
rather thancatch(SuperTypeOfHandledException e)
. – Dodgsonf
isnull
, the constructor ofNullPointerException
is called and the Constructor can throw aIOException
infillInStackTrace
. See community.oracle.com/thread/1445008?start=0 – Adalbert