Page 1 of 1

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

Posted: Sat Mar 02, 2013 5:51 am
by The_Nick
Hi,
I would like to point out that there is also this little trick besides the question scope.

Code: Select all

public static int doIt() throws Exception{
      throw new Exception("Forget It");
   }

If the question were something like: The method doIt() does not compile. true or false.?
What would you have replied?

Fantastic question so you can exercise also in tricky unreachable code things.
In this case the "return" statement must be omitted either before or after the Exception throwing itself.
If "return" had been used in either above or below the "throw new Exception" statement it would not have compiled at all as it would have given a compilation error due to unreachableness of the code.


The_Nick

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

Posted: Wed Jun 05, 2013 9:29 am
by bptoth
This question is quite similar to enthuware.ocajp.i.v7.2.1260; yet one of them is categorized into section 03 (operators) and the other into section 08 (exceptions)

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

Posted: Fri Sep 11, 2015 9:04 am
by Roibeard
public static int doIt() throws Exception{
System.out.println("Forget It");}
}
Won't compile because "This method must return a result of type int."
Why doesn't
public static int doIt() throws Exception{
throw new Exception("Forget It");}
}
have the same issue?

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

Posted: Fri Sep 11, 2015 10:48 am
by admin
Just the way the language designers chose to frame the language rules. The compiler knows that this call will always end abruptly and a return is not required.

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

Posted: Fri Sep 11, 2015 1:47 pm
by Roibeard
Ok,
thank you.

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

Posted: Sun Mar 19, 2017 4:52 am
by mcberenguer
I actually answered right (false) because I though it wasn't going to compile... a great question indeed, I learnt something new!

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

Posted: Fri Aug 06, 2021 8:07 am
by enthunoob
It wont even compile cause the top line writes a comment with only one slash. Right?

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

Posted: Fri Aug 06, 2021 8:23 am
by admin
Not really. It is a division operator. Can you post a screen shot of what you see?