aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception handling ques 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 "Exception handling ques" Watch "Exception handling ques" New topic
Author

Exception handling ques

Nikhil Bansal
Greenhorn

Joined: Mar 21, 2006
Posts: 21
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
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
<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.


-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling ques