• 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 HAndling

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class J5Q14{
public static void main(String a[]){
for(int i=0;i<10;++i){
try{
if(i % 3 ==0) throw new Exception("E0");
try{
if(i % 3 ==1) throw new Exception("E1");
System.out.println(i);
}catch(Exception ine){
i *= 2;
}finally{
++i;
}
}catch(Exception ine){
i *= 2;
}finally{
++i;
}
}
}
}
The options are
A 4
B 5
C 6
D 7
E 8
F 9
Ans are B and E
Please send the explanation
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class J5Q14{
public static void main(String Args[]){
for (int i=0; i< 10; ++i){
try{
if( i%3 ==0) throw new Exception("EO");// stat 1
try{
if(i%3==1) throw new Exception("E1");//stat 2
System.out.println(i);
}catch(Exception inner){ //stat 3
i*=2;
} finally {
++i;
}
}catch (Exception outer){ //stat4
i +=3;
}finally{

++i;
}
}
}
}
at stat1
//when i=0,it throws Exception and goes corresponding catch{Exception outer},then i=3 and then executes finally block,so now i=4;Now it goes in the for loop and i=5(because of ++i)
now 5%3!=0 so it does not throw exception at stat1,then the control moves to stat2
again 5%3!=1 so does not throw exception at stat2..
So prints 5
then it excecutes both the finally statments and the i =7,then it moves in the
for loop (before going to the loop ,++i is done) so i=8 and i%3!=0 so does not thrwo exception at stat1,the then the control moves to stat2
again 8%3!=1 so does not throw exception at stat2..
So prints 8.Again executes both the finally statements and the i=10 so does not
go in the loop

Hope that helps

Alpa-urja
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class J5Q14{
public static void main(String a[]){
for(int i=0;i<10;++i){
try{
if(i % 3 ==0) throw new Exception("E0");
try{
if(i % 3 ==1) throw new Exception("E1");
System.out.println(i);
}catch(Exception ine){
i *= 2;
}finally{
++i;
}
}catch(Exception ine){
i *= 2;
}finally{
++i;
}
}
}
}
The options are
A 4
B 5
C 6
D 7
E 8
F 9
Ans are B and E
Please send the explanation

First thing u might have left one option ie. 2
The correct output i think is :
2
5
8
1. In first iteration (inside for loop)
value of i=0
so 0%3=0, the condition if(i%3==o) is satisfied and it will throw an exception.
It will go to outer catch block so value of i=i*2=0
then it goes to outer finally block
where i is incremented by 1 so value of i=1;
Value of i will be incremented again coz of i++ in for loop
so now the value of i=2

2. In the second step the value of i=2, so first condition is not true so it will enter in inner try block.
so 2%3=2 so second condition is not satisfied so it will print 2
After that it will entry the inner finally block thereby increments i by 1 so i is now 3
After that it will entry the outer finally block thereby increments i by 1 so i is now 4
COz of i++ in for loop it will increment it by 1 so value of i is now 5
Since 5 does not satisfy both the conditions
System.out.pritnln(i) will execute thereby printing 5
The rest u can understand i think
so output should be:
2
5
8
bye

[This message has been edited by cgogoi (edited December 30, 2000).]
[This message has been edited by cgogoi (edited December 30, 2000).]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember that finally always executed.
u have 2 try block.if you enter just in outer you execute only the outer finally.if you
enter also in inner try, you'll execute both. so:
a.i=0;1st try block;exception thrown;don't enter 2nd try;catch outer i=3;finally outer i=4;
b.i=5(a & ++i from for statement);1st try block;enter the 2nd try;print 5(solution); inner finally i=6;outer finally i=7;
c.i=8(the same);1st try block;enter the 2nd try;print 8(solution); inner finally i=9;outer
finally i=10... The End.
hope i was clear.
rgds,
Cristi
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output from the program is
2
5
8
The 5 ,8 is understandable; could somebody explain why we're getting 2 ??
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,abhishek
The cgogoi's explaination can help you. I think the reason you dont understand why 2 is printed is you donot know the result of 2%3. since 2%3 = 2, the two conditional expression are false, so System.out.println(i) is excuted at last. so you can see 2 is
shown. Right?
regds
George
 
reply
    Bookmark Topic Watch Topic
  • New Topic