dcsimg
Thowing an Exception from RUN method of Thread
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nishant_Usapkar
Posted On:   Tuesday, September 11, 2001 02:41 AM

I have a method calling run method of Thread. The RUN method can throw a specific Exception


I want to catch this Exception in the calling method and not the RUN method itself

This is not possible though since one cannot overide RUN method as throws SOMEException


Is there any other way in which the calling method can know wheter the specific exception has occured inside the RUN method

Re: Thowing an Exception from RUN method of Thread

Posted By:   Alex_Chaffee  
Posted On:   Thursday, September 27, 2001 12:03 AM

That being said, there *is* a way to find out the exception
-- store it in an instance variable of the thread, and check the value after the thread exits.

class MyThread extends Thread {

Exception e;

public void run() {
try {
doStuff();
}
catch (Exception e) {
this.e = e;
}
}
}

Re: Thowing an Exception from RUN method of Thread

Posted By:   Kevin_Riff  
Posted On:   Saturday, September 15, 2001 06:43 PM

I can tell from your question that you don't understand the purpose of the run method.



You should never call run() directly. Doing so does not result in a new thread being started. Instead, you should call start() and let Java invoke the run method on a different thread.



The run method is very unusual in that there usually is no calling method. It's invoked by the JVM directly. If it could throw an exception, then it would pass directly to the JVM's default exception handler. This would be against Java's policy of requiring that the programmer deal with all checked exceptions that might occur.

About | Sitemap | Contact