Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B book self test 15 on page 409

 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused about answer B:
... adding a try/catch block around line 6 will cause compilation to fail

Line 6 is the last line of the code just containing the closing bracket of the class. Where is the try/catch supposed to go?

Thanks,

John
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have KB book around
Please be more specific with question.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:


And given the following four code fragments:

I. public static void Main(String[] args) {
II. public static void Main(String[] args) throws Exception {
III. public static void Main(String[] args) throws IOException {
IV. public static void Main(String[] args) throws RuntimeException {

If the four fragments are inserted independently at line 4, which are true ? (Choose all that apply)
A. All four will compile and execute without exception.
B. All four will compile and execute and throw an exception.
C. Some, but not all, will compile and execute without exception.
D. Some, but not all, will compile and execute and throw an exception.
E. When considering fragments II, III and IV, of those that will compile, adding a try/catch block around line 6 will cause compilation to fail.

In the answer it says:

E is incorrect because it's okay to both handle and declare an exception.

I just don't understand what 'adding a try/catch block around line 6' means. How would that look like?

Thanks,

John

 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to assume that this means within the main method there is a try-catch construct to check for division by zero. That would be "handling" the exception since there will be a divide by zero exception in this case. I believe something like this would be what they are talking about:

 
Christian Joseph
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe where on the same topic.... hmm, i dunno yet the answer

but the above post solve your problem about the QUESTION on the self Test
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm yes, I found this in the thread 'Compiling errata for K&B, SCJP 6'
John Summers wrote:page 409 question 15
a)says "at line 4" and should say "at line 2".
b)in option E I think it should say "line 4" not "line 6"
So maybe it is just an error?

John
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Stark wrote:Hm yes, I found this in the thread 'Compiling errata for K&B, SCJP 6'
John Summers wrote:page 409 question 15
a)says "at line 4" and should say "at line 2".
b)in option E I think it should say "line 4" not "line 6"
So maybe it is just an error?

John


I think perhaps after you write the code including the try and catch block you end up having the line you want inspected at line 6
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha ha, that's right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic