wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Exceptions 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" Watch "Doubt in Exceptions" New topic
Author

Doubt in Exceptions

Vidya Venkatesh
Greenhorn

Joined: Mar 06, 2003
Posts: 12
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
Your explanation seems reasonable enough for me Barry.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Vidya
I absolute agree what Barry said.



Francis Siu
SCJP, MCDBA
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
try {} catch (Exception e) {}
An exception (Exception or Error) is thrown for one of three reasons: See JLS 11.1
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
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. "
 
Consider Paul's rocket mass heater.
 
subject: Doubt in Exceptions
 
Similar Threads
exception java.lang.InterruptedException is never thrown in body of corresponding try statement
throw
Catching Exceptions
exam lab q-9(excepton handling)
Throwing exception in overriding methods