File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
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
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


geek@mantraonline.com
Peter Voorwinden
Greenhorn

Joined: Aug 02, 2000
Posts: 8
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
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
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
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://aspose.com/file-tools
 
subject: Exception
 
Similar Threads
question No.17,18 from Javaprepare
try/catch/finally
try, catch question
pl explain
5 javaprepare questions