• 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
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.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic