• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exception handling ques

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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 {
throw new Level1Exception();
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);
}}

Plz provide me what is exactly wrong with the above code.

Regards

Nikhil
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<quote>
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
{
throw new Level1Exception();
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);
}
}
</quote>


Hi Nikhil,
The problem is in the first line of the first try block "throw new Level1Exception();"

After You throw an exception in a code, the statements after the throws stmt will become unreachable by the compiler and hence, it fill flag an compile-time error.

-Dharmesh G.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic