aspose 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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exceptions" Watch "exceptions" New topic
Author

exceptions

aparna rudra
Greenhorn

Joined: Apr 06, 2004
Posts: 14
wpould anyone tell me wht is the output? and how does the control execution takes place.

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);
}}
Peter van de Riet
Ranch Hand

Joined: Apr 09, 2004
Posts: 112

output is 0,0,1,1,0,1
In the inner try is Level1Exception thrown. Because of this exception the next 2 case staments are not executed (break is missing, normally a fall-through would happen).
The Level1Exception is not caught in the catch clause from the inner try. The (inner try) finally of course is executed (c++). The catch Level1Exception from the outer try catches the exception and executes (d++). After a catch the next catches are not evaluated and the (outer try) finally is executed (g++).
The output therefore is as I wrote in the first line.

Peter


Each number system has exactly 10 different digits.
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Try modifying the code smth like this:

and then compile and run it. See what you get for yourself!
P.S. When posting a code, don't just paste it, but it in in the CODE UBB tags.
subu ananthram
Ranch Hand

Joined: May 16, 2004
Posts: 102
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);
}}

initially x=1 enters nested try
and throws Level1Exception() then you look for a catch block to handle that exception no such catch implement finally goes to outer catch handles exception implements finally
so you get the answer as 001101
i hope that answers your question
regards
priya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exceptions