aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Exceptions - exception never thrown in body of corresponding try stmt 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 "Doubt in Exceptions - exception never thrown in body of corresponding try stmt" Watch "Doubt in Exceptions - exception never thrown in body of corresponding try stmt" New topic
Author

Doubt in Exceptions - exception never thrown in body of corresponding try stmt

Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
class X extends Exception{}
class Z
{
static Object obj=null;
public static void main(String args[])
{
dun();

}
static int dun()
{
try
{
}
catch(X c)
{
}
catch(Exception e)
{
}
return 1;
}
}

This gives compiler error that X is never thrown

But the code:
class Z
{
static Object obj=null;
public static void main(String args[])
{
dun();

}
static int dun()
{
try
{
}
catch(Exception e)
{
}
return 1;
}
}
Comiles fine....How? Eventhough in try block exception is never thrown
Bilal Sallakh
Ranch Hand

Joined: Feb 26, 2005
Posts: 45
Exception is a superclass of RuntimeException which needen't be caught and a programmer can throw it without declaring the enclosing method to throw it.

Even if no real code is inside (which is trivial, mind you) there may be commented one. It's not reasonable to enforce the programmer to remove the catch block or the try statement when there is no problematic instruction and return it when there is so. The compiler for sure will ignore the entire block...


SCJP for J2SE 5.0 (beta)<br />SCJA (beta)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
didn't clear bilal ???

any other answer...
good question..


Thanks and Regards, Amit Taneja
vijay_kumar
Greenhorn

Joined: Jun 07, 2005
Posts: 1
User defined exceptions should be thrown otherwise compilation error comes.Because JVM doesn't know when to throw user defined exceptions.An Exception is a class known to JVM and so it accepts Exception in catch block even though we dont put any code in try block.
Bilal Sallakh
Ranch Hand

Joined: Feb 26, 2005
Posts: 45
Try subclassing Z from RuntimeException instead of Exception. The code will compile.
As you know, in Java RuntimeException and its subclasses are not checked, i.e. can be thrown without being caught.
So a normal code can throw a runtime exception for example:

int[] a = new int[5];
a[5] = 6;

or

int b = 0;
int d = 1 / b;

Both code can throw a RuntimeException which needn't be caught.

But you can catch a RuntimeException for sure by

try {
int b = 0;
int d = 1 / b
}
catch (RuntimeException e) {
// handling the exception
}


Notice that any normal code even if that doesn't throw a checked exception - an exception which is not a subclass of RuntimeException - can be put in a try block followed by a catch (RuntimeException e) {} block. But since Exception is a superclass of RuntimeException, and since everything caught by X can be caught by the superclass of X, we can put any code in a try block followed by a catch (Exception e) {} block.

Notice that Z is not a superclass of RuntimeException (nor a subclass) , so the above argument is not valid for it. The only justification for catching it is the possiblity of it be thrown and this is only possible if the code throws it explicity or call a method that declare that it may throw it.

Notice that an empty code is a normal code
(if you read about how compilers work you find a rule a BNF rule like
statement -> IFStatement | ForStatement | CompoundStatement
CompoundStatement -> { statement* } // a repetition of 0 or more statement...
)
So in theory an empty statement is a statement, and any statement is allowed to throw a RuntimeExecption without declaring so, so the codes is valid.
In practice any compiler ignore this statement entirely (with its try and catch). Moreover, I think if the compiler can make sure that the a block can't throw any RuntimeException, it will ignore all the relevant catchs...

Hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Exceptions - exception never thrown in body of corresponding try stmt