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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick

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

Post 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

bptoth
Posts: 33
Joined: Mon May 06, 2013 9:41 am
Contact:

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

Post 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)

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Ok,
thank you.

mcberenguer
Posts: 4
Joined: Thu Mar 02, 2017 12:00 pm
Contact:

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

Post by mcberenguer »

I actually answered right (false) because I though it wasn't going to compile... a great question indeed, I learnt something new!

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

It wont even compile cause the top line writes a comment with only one slash. Right?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Not really. It is a division operator. Can you post a screen shot of what you see?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests