java - Eclipse. Why don't I see compile error if method with unreachable code doesn't invoke? -
i noticed if method unreachable code doesn't invoke - code compiles eclipse compiler , executes.
demonstration:
import java.io.filenotfoundexception; class r3 { public void g(){ try { } catch (filenotfoundexception e) {//any checked exception } } public static void main(string [] args) { system.out.println("23"); new r3().g(); } }
result:
unreachable catch block filenotfoundexception. exception never thrown try statement body
and compare following code:
import java.io.filenotfoundexception; class r3 { public void g(){ try { } catch (filenotfoundexception e) {//any checked exception } } public static void main(string [] args) { system.out.println("23"); //new r3().g(); } }
compiles , executes normally.
is eclipse compiler optimization or normal behaviour?
this contribute discussion. when compiled eclipse , decompiled online, got. reason eclipse decides compile method throw runtime error there unresolved compilation problem? interesting.
class r3 { public void g() { throw new error("unresolved compilation problem: \n\tunreachable catch block filenotfoundexception. exception never thrown try statement body\n"); } public static void main(string[] args) { system.out.println("23"); } }
i found somewhere else on so, explains happens pretty clearly.
one notable difference eclipse compiler lets run code didn't compile. if block of code error never ran, program run fine. otherwise throw exception indicating tried run code doesn't compile.
Comments
Post a Comment