Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't clear bilal ???

any other answer...
good question..
 
vijay_kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic