This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's question 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 "Dan Watch "Dan New topic
Author

Dan's question

jioy uilo
Greenhorn

Joined: Mar 29, 2003
Posts: 25
Hi dan
Can u pls explain this code to me ? confused...
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 = 1;
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);
}
}
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch, jioy.
Finally clauses always execute regardless of the throwing or not of an exception in try's . In this case "finally{ c++; } is executed after "catch(Level2Exception e)" failed to catch the thrown Level1Exception. Then "catch(Level1Exception e)" catches the exception. Eventually "finally{g++};" gets executed.


SCJP2. Please Indent your code using UBB Code
jioy uilo
Greenhorn

Joined: Mar 29, 2003
Posts: 25
try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}

how come above segment compiled ? i tot that, a try statement shld end with a catch or finally clause ?
how did a++ managed to fit in ?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi jioy
well, it does end at appropriate place. the "}" u see if of "switch" not of TRY...
regards
maulin
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi jioy
Let me add something according to Jose
I trace the coding once,because ensure the coding without compiler error.

Hope this help


Francis Siu
SCJP, MCDBA
jioy uilo
Greenhorn

Joined: Mar 29, 2003
Posts: 25
Thanks Siu
I was caught in Dan's trap !!
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi jioy
I was caught in Dan's trap !!

Actually,when you do some Dan's mock question,make sure to look the coding carefully that without compiler error.
So,we should know how to do the compiler job,or to be a real compiler
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's question
 
Similar Threads
unreachable try{
exceptions
exception
exceptions
Exception handling ques