• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Following Try Catch Finally Structure & Wrong SybexTestBank Answer  RSS feed

 
chris stahle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am preparing to take my 1z0-808 test this weekend and I have been making pretty good test scores on whizlabs.com as well as Sybextestbanks.com.
I came across a problem in the Sybex website and I figured I should utilize the forum.

The answer listed is that this prints "abce" and a RuntimeException with the message of "3".
Could somebody take a look at my attachment and tell me why that is ?

Tracing the code in my head it, it would makes sense to me to follow the code in this order :
1.) print "a"

2.) print "b"
3.) throw a new IllegalArgumentException

4.) catch that IllegalArgumentException
5.) print "c"
6.) throw new RuntimeException with message set to "1"

7.) catch that RuntimeException
8.) print "d"
9.) throw new RuntimeException with message set to "2"

10.) print "e"
11.) throw new RuntimeException with message set to "3"

12.) we can't catch that RuntimeException, dump that message to the screen 

Why, following the lines on my attachment, does line 19 not take you into line 20?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Since I'm already here I thought I should point out an wrong answer over at Sybex.

Take a look at my second attachment if you are studying from this website.
It's wrong because it incorrectly says the answer is B,D.
The correct answer is that none of those examples yield true and comparing a StringBuilder object to a String literal by == doesn't even compile.
order-of-operations-catch-blocks.PNG
[Thumbnail for order-of-operations-catch-blocks.PNG]
Try catch finally structure
WRONG-ANSWER-SYBEX.PNG
[Thumbnail for WRONG-ANSWER-SYBEX.PNG]
a wrong answer regarding StringBuilder comparison
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Chris,

welcome to the ranch!

Fist of all, this assignment is from the Sybex OCAJP book by Boyarski and Selikoff, chapter 6, assignment 20.

Your reasoning is okay, up to step 7. In a try.. catch sequence only one catch will be invoked, at the most. So, when in the IllegalArgumentException catch, an RTE is thrown, then there is no catch to catch that Exception!
So, an uncaught RTE("1") is then thrown. However, in the finally clause, yet another Exception is thrown, and that exception hides the RTE("1"").
Therefore, the outcome is indeed "abce" together with the error that is thrown in the finally clause.

Indeed a question better not raised at the exam itself... but then again, this example is one you will never forget, so come up with that exam!

Meanwhile, try to experiment a little and see the outcomes. For instance (remove the outcommented parts) to see the effect:
 
chris stahle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhhhhhhhhhhhhhhhhhhhhhhhhh! Thank you for helping me understand. The example code was very helpful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!