Page 1 of 1

About Question enthuware.ocajp.i.v7.2.964 :

Posted: Mon Oct 08, 2012 9:23 am
by ETS User
System.out.println("c") is always unreachable , so why not it cause compiler error.
It the answer is: The compiler cannot decide during compile time that the statement is unreachable, then how do we decide if the compiler will be able to decide or not during compile time?

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Mon Oct 08, 2012 9:43 am
by admin
The detailed rules are given here: Section 14.21 of JLS

It may seem complicated but the general idea is to look for compile time constants i.e. final variables and literals. For example,

Code: Select all

int x = 5;
while (x > 100){ System.out.println(x); }
will work. But this will not:

Code: Select all

final int x = 5;
while (x > 100){ System.out.println(x); }
because compiler knows that x cannot change from 5.

On top of this, there are some exceptions such as the if statement. It is explained in the above mentioned section.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Fri Nov 15, 2013 7:00 pm
by jeremy_dd
I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Fri Nov 15, 2013 7:09 pm
by admin
jeremy_dd wrote:I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
The question and the explanation are correct. Please make sure you paste the code exactly as given.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Thu Jun 16, 2016 6:00 am
by KKreator
jeremy_dd wrote:I pasted the code in my eclipse, it fails to compile because m1 is supposed to throw an exception, which is not caught...
I think you forgot to add "...throws Exception" to the main function.

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Mon Jun 05, 2017 2:35 am
by JavaSoftware
Hi! But if we catch the exception in catch block and print some message say "message" , then the statement System.out.println("C"); will be executed ?

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Mon Jun 05, 2017 2:42 am
by admin
What happened when you tried it out?
Paul.

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Mon Jun 05, 2017 4:10 am
by JavaSoftware
Thank you for your promotion to write and test code by myself and sorry for my hastiness.
And the statement System.out.println("C"); will be printed after we catch the exception :)

Re: About Question enthuware.ocajp.i.v7.2.964 :

Posted: Mon Jun 05, 2017 4:13 am
by admin
Very good :)
This exam is all about writing short code examples to understand the basic concepts.