Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exceptions

 
aparna rudra
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic