Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Roundup question #132

 
Greg Higgins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is an error with the following question and answer.
(#132) TRUE or FALSE: if an exception is not caught, the finally block will run and the rest of the method is skipped.
Answer: TRUE The finally block will always run if an exception is thrown, and then the exception is immediately passed to the calling method.
The question states if an exceptions is �not� caught, but the answer states what occurs if an exception is thrown.
If an exception is caught, the finally block will run and the remainder of the method is skipped. However, if an exception is �not� caught, the finally block will run, but the remainder of the method is will not be skipped (unless the finally block does something to halt execution).
 
paul wheaton
Trailboss
Pie
Posts: 21597
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an exception is not caught, then the finally block is run and then the exception is passed to the calling method. The rest of the code in the method is not run.
 
Greg Higgins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I misunderstood the question. I believe the answer should be FALSE. I created and ran the following test program:
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(false);
}
public void test(boolean throwException) {
try {
throwException(throwException);
} catch (Exception e) {
System.out.println(e.getMessage());
return;
} finally {
System.out.println("executed finally");
}
System.out.println("after the block");
}
public void throwException(boolean throwException) throws Exception {
if(throwException)
throw new Exception("exception thrown");
}
}
The resulting output is:
executed finally
after the block
An exception was not caught, and the remainder of the method was not skipped, thus the line �after the block� was printed. Changing the parameter to true will cause the remainder of the method to be skipped and the following output:
exception thrown
executed finally
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The confusion here is over the statement "an exception was not caught" This can be slightly ambiguous because does it mean
1. An exception was thrown but not caught
or
2. An exception was not thrown and therefore not caught
Generally speaking, when I have seen "an exception was not caught", I take it as an exception was thrown but not caught becuase the other version wouldn't have an exception in the first place so it would say "an exception was not thrown" or "the try block did not generate an exception." By saying "an exception was not caught" I think it implies that there is an exception, it just wasn't caught.
The real exam is more definative on this I believe and will say whether or not an exception was generated.
Bill
 
Greg Higgins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation. When I read the question my assumption was that there was no exception thrown, which is why I thought there was an error here. The ambiguity got me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic