Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Exceptions

 
Vidya Venkatesh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi when I try to compile this piece of code it gives me the error "InterruptedException is never thrown in this block".
try
{
// code which does not throw Exception
}
catch(InterruptedException e)
{}
But if I change InterruptedException to Exception here, then it compiles. Why is this happening ?
It should have given an error "Exception is never thrown in this block". Please advise,
thanx
Vidya
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I convinced myself that the explanation for this is:
Exception is a superclass of RuntimeException. RuntimeException and its subclasses are "unchecked" exceptions which do not have to be declared as being thrown.
So if the try block contained a call to a method which throws an undeclared RuntimeException, then it would be caught in the catch (Exception e) block.
An empty try block could "throw" an undeclared runtime exception, if the JVM freaks out.

Therefore the compiler cannot complain in this case.
I guess someone has a better explanation.
[ April 06, 2003: Message edited by: Barry Gaunt ]
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your explanation seems reasonable enough for me Barry.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Vidya
I absolute agree what Barry said.

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try {} catch (InterruptedException e) {}
A compile-time error occurs because the catch clause is unreachable. (JLS 14.20)
The catch clause is unreachable because there is no expression or throw statement in the try block that can throw an exception whose type is assignable to InterruptedException. (JLS 14.20)
For the same reason, a compile-time error should occur in the next example. But a compile-time error does not occur.
try {}
catch (NullPointerException e) {}
catch (RuntimeException e) {}
catch (Exception e) {}
catch (Error e) {}
catch (Throwable e) {}
There is nothing in the JLS that says unchecked exceptions are exempt from JLS 14.20.
Look at the following links and you will see this is either a JLS bug or a compiler bug.
http://www.ergnosis.com/java-spec-report/java-language/jls-14.20-c.html
http://developer.java.sun.com/developer/bugParade/bugs/4046575.html
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try {} catch (Exception e) {}
An exception (Exception or Error) is thrown for one of three reasons: See JLS 11.1
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:
For the same reason, a compile-time error should occur in the next example. But a compile-time error does not occur.
try {}
catch (NullPointerException e) {}
catch (RuntimeException e) {}
catch (Exception e) {}
catch (Error e) {}
catch (Throwable e) {}

Strange, if I had been the compiler I had thought to myself: "This code is useless, what could possibly happen here, I could just remove the hole thing, but lets complain to the programmer instead. "
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic