This content has been marked as final. Show 6 replies
I have read the basic stuff of Java but still...If the compiler says the exception can't be thrown, then it can't be thrown.
I still think that the try-catch is needed because
the method calling the ThreadAction has possibility
Please tell me what's wrong.
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.
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.
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.