This is a question from javaprepare site What is the output of the following program public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i/j > 1) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } }
A 0 B 1 C 2 D 3 E 4 The answer given are A,D,E. Dividing by 0 will result in Arithmetic Exception therefore A is correct. D and E are correct because finally block will always get executed and the last statement is outside try and catch block.I want to know why C is incorrect. Arithmetic Exception extends Runtime Exception which extends Exception. So C should be a valid answer.
Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Geek
Greenhorn
Joined: Sep 09, 2000
Posts: 21
posted
0
Snigs, what you are saying is correct, but you are missing one point. Once an exception is caught(in this case ArithmeticException) it does not check for other exceptions in the same try block. Had the provision for catching ArithmeticException been not there, Exception would have been caught and 2 would have been printed instead of zero. Geek
Be careful: suppose the exception was not caught by one of the catch blocks, the finally block would get executed, but the line(s) after the try/catch/finally would not get executed, but control flow would jump to the end of the method. Just FYI. Peter
Snigdha Solanki
Ranch Hand
Joined: Sep 07, 2000
Posts: 128
posted
0
thanks for the explanation
Originally posted by Peter Voorwinden: Be careful: suppose the exception was not caught by one of the catch blocks, the finally block would get executed, but the line(s) after the try/catch/finally would [b]not get executed, but control flow would jump to the end of the method. Peter[/B]
I think irrespective of whether the exception got caught or not the line after try/catch/finally will be executed. I tried it and it worked
vidhya Ramachandran
Greenhorn
Joined: Sep 15, 2000
Posts: 13
posted
0
Actually if the exception was not caught (there is no corresponding catch block), only the finally block will be executed. The output is: 3 java.lang.ArithmeticException: / by zero at test3.main(test3.java:8) Exception in thread "main" Process Exit...
[This message has been edited by vidhya Ramachandran (edited September 22, 2000).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
if the Exception is not caught then the statement in finally and the statement after the finally will be excuted. The output for the following code is: 3 4
public class test { public static void main(String args[]) { int i=1, j=1; try { //i++; i and j increments commented out //j--; if(i/j > 1) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } }
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.