• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Question from 4tests.com

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void divide(int a, int b) {
try {
int c = a / b;
}
catch (Exception e) {
System.out.print("Exception ");
} finally {
System.out.println("Finally");
}
a) Prints out: Finally
b) Prints out: Exception
c) Prints out: Exception Finally
d) No output
I thought the answer is (a) but it is (c), can someone pls explain?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi geeta, none of the answers are correct because the resulting output is unpredictable (it depends on the parameter of the 'divide' method).
For example:
If we call divide(10,10) the output will be finally
if we cal divide (10,0) the output will be Exception Finally
The finally statement is always executed disregarding if it triggers an exception or not.
The answer is: Non of the above
Hope this helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic