Thread procedure just claims that InterruptedException is never thrown

Hello!

I have a simple thread procedure that persitantly gives errors about not throwing InterruptedExceptions.

exception java.lang.InterruptedException is never thrown in body of correspondingtry statement

I have tried to addcatch statements andthrows InterruptedException definitions to my code into several places without success.

Please, I would highly appreciate if you could point me in detail all the rows in my code where I should put some new statements and definitions.I have already tried to add throws InterruptedException definitions to all methods that are "above" the method that calls ThreadAccess class. But still the original error message appears.

Here are the main parts of my code (without some method between main and mouseClickedOnCorrectJPanel):

privatestaticclass ThreadActionimplements Runnable{

publicvoid run(){

try{

animateJPanel();

}catch (InterruptedException e){

System.out.println("Error message");

}

}

}

...

privatevoid mouseClickedOnCorrectJPanel(){

try{

startingTime = System.currentTimeMillis();

Thread t =new Thread(new ThreadAction());

t.start();

while (t.isAlive()){

t.join(timeIntervalForCheckingIsThreadStillAlive);

if (((System.currentTime() - startingTime) > patientWaitingTimeForThread) && t.isAlive()){

t.interrupt();

t.join();

}

}

playMusic();

}catch (InterruptedException e){

System.out.println("interrupt. exception ilmestyi");

}

}

...

class MyApplication

{

publicstaticvoid main(String[] args)throws IOException{

try{

JFrame frame =new JFrame("MyApplication");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SwingApplication app =new SwingApplication();

app.addComponentToPane(frame.getContentPane());

frame.pack();

frame.setVisible(true);

}

finally{

System.out.println("Finished");

}

}

}

Thank you for helping!!

-Wonderful-

[3855 byte] By [wonderful123a] at [2007-11-27 10:21:21]
# 1

Remove the try/catch(InterruptedException). If the compiler is saying it's never thrown, then don't try to catch it.

It's basic stuff, read the basics before trying to do threads.

-Kayaman-a at 2007-7-28 17:09:29 > top of Java-index,Java Essentials,Java Programming...
# 2

I have read the basic stuff of Java but still...

I still think that the try-catch is needed because the method calling the ThreadAction has possibility to launch

t.interrupt();

Please tell me what's wrong.

-wonder-ing

wonderful123a at 2007-7-28 17:09:29 > top of Java-index,Java Essentials,Java Programming...
# 3

> I have read the basic stuff of Java but still...

>

> I still think that the try-catch is needed because

> the method calling the ThreadAction has possibility

> to launch

>

> t.interrupt();

>

> Please tell me what's wrong.

If the compiler says the exception can't be thrown, then it can't be thrown.

Tell me which exact line in your code you think can throw it.

Also, nothing "calls" ThreadAction. ThreadAction is a class. We don't call classes. We call methods.

jverda at 2007-7-28 17:09:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Perhaps you think the newly created thread can throw that exception back to the thread that spawned it? It doesn't work that way. The thread that launched the other thread could be anywhere--it could be right after the start call, it could be a few methods later, it could have ended (except that you put join, so it will wait for the spawned thread to die first).

That's also why you can't return a value to the spawning thread, and it's fundamental to the whole POINT of threads. They run independently. Unless you take steps to synchronize them--wait, join, etc.--you can't control what one thread is doing when another thread gets to a certain point.

jverda at 2007-7-28 17:09:29 > top of Java-index,Java Essentials,Java Programming...
# 5

Interupt only causes an exception if the interrupted thread is in a wait() or sleep() call. Otherwise it simply sets a flag, which you should test when you loop. (If the flag is set and you call wait or sleep an exception is immediately thrown.

Hence you only need to catch InterruptedException if your thread code calls wait or sleep.

malcolmmca at 2007-7-28 17:09:29 > top of Java-index,Java Essentials,Java Programming...
# 6

Thank you very much for very detailed answers!!!!

:)

Wondrefull......

wonderful123a at 2007-7-28 17:09:29 > top of Java-index,Java Essentials,Java Programming...