• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try, catch question

 
Jay Ashar
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


A 0
B 1
C 2
D 3
E 4

what will be the output of this code... this mock exam answer says ADE
[ Jess added UBB [code] tags to preserve whitespace. Check 'em out! ]
[ October 31, 2002: Message edited by: Jessica Sant ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try running it??
The output of the program is 034, just like the mock exam answer.
during the first iteration of the code 'i' goes up to 2, 'j' goes down to 0
i/j ( -> 2/0) produces an ArithmeticException: Divide By Zero
which is caught by the catch (ArithmeticException e) and prints '0'
the finally is always executed and prints '3'
then the code exits the try/catch and prints '4'
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try compiling and running it? That's the best way to understand the answers to questions like this.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic