• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Exceptions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your explanation seems reasonable enough for me Barry.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vidya
I absolute agree what Barry said.

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. "
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic