Home arrow static arrow Java Programming [Archive] - Throws Exception
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Throws Exception
This topic has 3 replies on 1 page.

Posts:3
Registered: 3/18/04
Throws Exception  
Jul 11, 2004 9:03 PM



 
Dear Expert,

May I know how to throw Exception from a thread back to calling program ?
As I always get the following message when compiling the code

cannot override run() in java.lang.Thread; overridden method does not throw java.lang.Exception public void run() throws Exception

Thanks!

w
 

Posts:408
Registered: 1/22/99
Re: Throws Exception  
Jul 11, 2004 11:29 PM (reply 1 of 3)



 
Instead of throwing checked exception like java.lang.Exception throw unchecked exception like java.lang.RuntimeException. Then you do not need to declare them in throws clause but you can throw them within your code and catch in caller.

But still you have a problem how to catch exception throwed in separate thread. These exceptions should be handled inside run() method. In fact you never call run() directly (of course you can) but you call Thread::start().
 

Posts:100
Registered: 1/26/02
Re: Throws Exception  
Jul 12, 2004 1:44 AM (reply 2 of 3)



 
You cannot throw back the exception from a thread to the calling thread. Instead, use, interthread communication and synchronization (using common variable - wait, notify) to inform / transfer the data
 

Posts:408
Registered: 1/22/99
Re: Throws Exception  
Jul 12, 2004 2:30 AM (reply 3 of 3)



 
I believe there is a simple thing you can do: just pass reference to instance starting thread and call some method on caller when exception is thrown in thread. Some like:

public synchronized threadExceptionThrown(){	do something when exception in thread} ... {	new MyThread(this).start();} .... public void run(){	try{...}	cathc(Exception e)	{		referenceToCaller.threadExceptionThrown();	}} 


It is similar approach used with all listeners used in GUI applications and beans.
 
This topic has 3 replies on 1 page.