• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock 3 exam wrong answer (Java OCA 8 Programmer I Study Guide)

 
Greenhorn
Posts: 9
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the questions is about what exception can fill in the blank line to make the code compile.



In the answer option D (LimpException) is said to be incorrect because the method split throws HurtException and no other exception. Therefore catching LimpException would be unreachable code because HurtException is already caugth.
But I can compile following code, so answer D is correct. I do receive a warning in my IDE about unreachable code but the source compiles and runs.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vince Botteman wrote:In the answer option D (LimpException) is said to be incorrect because the method split throws HurtException and no other exception. Therefore catching LimpException would be unreachable code because HurtException is already caugth.
But I can compile following code, so answer D is correct. I do receive a warning in my IDE about unreachable code but the source compiles and runs.


First of all, I completely agree with you about answer option D: it should be a correct answer because if LimpException fills in the blank, the code compiles successfully (although with a warning but that's not a compiler error) and you can run the program as well.

But you have to be very careful: there's a huge difference between "unreachable code" and "dead code"! The first one results in a compiler error, the latter one only in a warning. So on the exam, you'll have to know the difference between both, because with dead code the compiler will successully compile the code (and thus you can run the code); but when it detects unreachable, it gives a compiler error and you won't have a .class file (and thus you can't run the code).
To illustrate consider these 2 code snippetsThe distinction between "unreachable code" and "dead code" is sometimes very hard. So you'll find plenty of threads on this forum about this topic (most of them with lots of examples). E.g. here, here and here.

So the explanation in the book mentions that Java recognizes the LimpException as dead code (which is correct), but dead code is only a compiler warning not a compiler error. So answer D is correct! And finally (pun intended ), I must say I'm a little bit surprised with the compiler warning you'll get when you compile the Mock3ExamHurtException class:
Mock3ExamHurtException.java:10: warning: unreachable catch clause
catch (LimpException e) {System.out.println("Limp");}
^
thrown type HurtException has already been caught
1 warning

It mentions "unreachable" so that could be very confusing and misleading, because unreachable code is a compiler error (and here you'll get a compiler warning).

Hope it helps!
Kind regards,
Roel
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right! Added to the errata. I was so sure that LimpException wouldn't work that I didn't actually try it. Our tech editors must have been as well. Trying it now, I totally agree.

I gave you both a cow for this one. Because this wasn't a typo or slip of the mind. I really didn't think it would work.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A similar example with 2 classes from the Java API: FileNotFoundException extends IOException. Given this method declarationthis code snippet compiles with a warning

And what do you think will happen when you compile the previous code snippet after you have changed the method declaration to Will it still successfully compile? With or without warnings?
 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the code will not compile because FileNotFoundException is a checked exception and FileNotFoundException is not a superclass of the class that is declared to be thrown by the method (i.c. IOException). So the compiler will detect that FileNotFoundException or a subclass will never be thrown in the try block.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vince Botteman wrote:I think the code will not compile


Wrong! The code compiles successfully, even without any warning. So it's perfectly valid code.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel: That's evil!

Vince: The reason is that doRiskyThings() declares IOException. The actual implementation is free to throw IOException, any subclass of IOException, any runtime exception or no exception at all. Since it is possible for method to throw a FileNotFoundException, the code is legal.
 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for the clarification. It takes my knowledge of exceptions one step further.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Roel: That's evil!


That's my middle name!

Vince Botteman wrote:It takes my knowledge of exceptions one step further.


Let's see if we can increase this knowledge about exception a little further.

Given this class hierarchywhich of these methods will not compile and/or not run successfully? And why not?

Hope it helps!
Kind regards,
Roel
 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Given this class hierarchy
...
which of these methods will not compile and/or not run successfully? And why not?
...


It is a perfect example, Roel, thanks a lot. I learn new things which I don't know so far but I can't keep all of them in memory from the first time It is interesting that why don't you write study guide about certification?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:It is interesting that why don't you write study guide about certification?


Writing a study guide from scratch is a very daunting task and that requires a massive amount of time and dedication
 
Vincent Botteman
Greenhorn
Posts: 9
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

The answers of the quiz:


It compiles successfully because the checked exception FoodException is caught. No problems at runtime.


At compile time the reference a is an Animal. And the exception FoodException is not caught or declared to be thrown. So compile error.


Similar to case 1: no compile or runtime errors


The reference is cast to Animal and the method Animal.eat throws FoodException which is not caught or declared to be thrown, so compile error.


The reference is cast to Tiger and the method Tiger.eat throws VeggieException which is a subclass of Exception thrown by method5, so the source compiles. But at runtime it will throw a ClassCastException because the object the reference a is pointing to is not a Tiger object.


The reference a is cast to Elephant and the method Elephant.eat throws no exception. It will compile and run successfully. A method is free to declare to throw an exception without actually throwing it.


This time it fails to compile because the catch block is unreachable.

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vince Botteman wrote:The answers of the quiz:


A perfect score! Have a cow for having all the correct answers and providing each time a spot-on explanation
 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This time it fails to compile because the catch block is unreachable.


Just to confirm my own understanding, the reason the catch block is unreachable is that the Elephant eat method implementation doesn't throw the FoodException right? Since we know that the compiler knows it is an Elephant reference (due to the Elephant cast), it uses that method implementation to determine that the code block is unreachable.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris: That is correct! Good explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic