*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception HAndling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception HAndling" Watch "Exception HAndling" New topic
Author

Exception HAndling

NikhilNandu
Greenhorn

Joined: Dec 26, 2000
Posts: 2
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
alpa urja
Greenhorn

Joined: Dec 27, 2000
Posts: 17
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
cgogoi
Greenhorn

Joined: Dec 30, 2000
Posts: 2
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).]
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
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
abhishek
Greenhorn

Joined: Dec 16, 2000
Posts: 5
The output from the program is
2
5
8
The 5 ,8 is understandable; could somebody explain why we're getting 2 ??
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception HAndling
 
Similar Threads
unreachable try{
try/catch
exception handling
try-catch-finally
exceptions