• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic