• 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

Jammi Question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public static void main(String Args[]){
for (int i=0; i< 10; ++i){
try{
if( i%3 ==0) throw new Exception("EO");
try{
if(i%3==1) throw new Exception("E1");
System.out.println(i);
}catch(Exception inner){
i*=2;
} finally {
++i;
}
}catch (Exception outer){
i +=3;
}finally{
++i;
}
}
}
}
(choice: 4,5,6,7,8,9)The ans is 5 and 8.
Can anyone explain this in detail for me?
Thanks
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is your code and below is the explanation
class Test{
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
 
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic