Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Help Following Try Catch Finally Structure & Wrong SybexTestBank Answer

 
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
 
Bartender
Posts: 4001
156
  • 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.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic