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 exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exceptions" Watch "exceptions" New topic
Author

exceptions

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
can anyone explain this code step by step how the control passes
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 3;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
O/P is 0,1,1,0,0,1

and in the same way i suppose the next code below
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 2;
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}

O/P same 0,1,1,0,0,1


Harish<br />SCJP 1.4 (85%)
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Ex1
1- x=3 so entering "case 3" -> throw new Level3Exception();
2- catch(Level2Exception ex) in the inner try block because "Level3Exception is a Level2Exception"
3- b++ -> b=1
4- doing finally of the inner try block : c++ -> c=1
5- doing finally of the outer try block: g++ > g=1
6- output

Ex2
You're right, it's the same flow.


Regards,<br />Seb<br /> <br />SCJP 1.4
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
hey thanks, gotta know aftr u explained,didnt go thru the code properly
anyway thanks
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979


Hope you got it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exceptions