File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception treated differently 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 "Exception treated differently" Watch "Exception treated differently" New topic

Exception treated differently

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello all,
This is about Threads. The wait(), notify(), and notifyAll() method should be included inside a synchronized block/method and in try block. My question is why does it give a runtime error only when the catchint exception is 'InterruptedException'. I tried with catch block like this,
catch(Exception e) but this doesn't give a run time error. Could you please explain this for me.
Thank you very much
public class A extends Thread{
public static void main(String ar[]) {
A a = new A();

public void run() {
for (int x = 0 ;x<2;x++) {

try {
}catch(InterruptedException e ) {} // If it is Exception , it doesn't // give runtime exception


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
InterruptedException is a subclass of Exception.

Ana<p>SCJP 1.4
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Ransika,
Here point is, when you call wait(), notify() or notifyAll() methods from ourside of syncronized methods/blocks, compiler will throw IllegalMonitorStateException, and this exception is subclass of Exception and not checked exception.Hence,
1. When you call these methods outside the synchronized block/method, you are not getting the compile time error(Bec, IllegalMonitorStateException is not checked exception).
2. IllegalMonitorStateException is not subclass of InterruptedException, so you can't catch IllegalMonitorStateException with catch(InterruptedException) method.
3. When you use, catch(Exception e), you can very well catch the exception, IllegalMonitorStateException and can proceede with normal execution.
Hope it calrifies your doubts,

I agree. Here's the link:
subject: Exception treated differently
It's not a secret anymore!