Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question in finally

 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers
I came accross a question (given below) while preparing for scjp .The question goes like this

Which statement is true?
A A try statement must have at least one corresponding catch block.
B Multiple catch statements can catch the same class of exception more than once.
C An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
D Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
E Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block must always run to completion.

Answer: E

I think the answer given by author is incorrect (to my knowledge it will be option
D
)because we can't be sure of completion of finally block as it may also throw any exception of it own.What you guys think about it ?.
 
Simeon Cherniy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of cource, we can't be sure of completion of finally block . But the finally block will be executing until it is not finished. If finally block throw exception than on the top of stack trace we will have this exception
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Simeon Cherniy:
Of cource, we can't be sure of completion of finally block . But the finally block will be executing until it is not finished. If finally block throw exception than on the top of stack trace we will have this exception


Simeon, check this code:



The output is "was thrown in finally" and nothing more.

So, the author of this topic is absolutely right, the answer should be "D"
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ivan
 
joy peter
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Answer D makes more sence since we cannot control finish of finally rather than start E sounds bit odd.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic