Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Snigdha Solanki
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Geek
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Voorwinden
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic