How can I throw an exception back from a thread.


I am relatively new to using thread but everything seemed to be ok until I tried to throw an exception back to my application from my new thread.

I thaught it would be simple

public MyThread extends Thread implements Runnable
{
..
    public void run() throws Exception
    {
        .... do stuff ....
   
        //error occurs
        throw MyException("My Error message");
    }
}

But it seems java wont let the run method of a Thread to throw an excepotion. :-(

So what are my options.. I want to throw the exception so I can catch it within the application, stop a progress bar and open an error dialog.
col_harrisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OviCommented:
Since the exception is thrown in a separate thread it cannot be intercepted by the main application thread. You have some solution to this as follows:
1. don't throw the exception inside the run method, it cannot be monitored from outside. The exception should be thrown in a method accesible to the your application classes outside the thread.
2. When you start the thread you should pass a claaback interface. Instead of throwing exception, make a call to the callback method to announce your application.
3. You can use event driven implementation and notify the main application thread about an error with a event.

Depending on your application, there can be more solutions and a finest refinement of what I've allready proposed. Waiting for more details.
0
functionpointerCommented:

Check out java.util.Observer and java.util.Observable.

Instead of throwing an exception, you notify.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SeaCosmosCommented:
Be sure that the run method is not invoked by your code,when you wanna create a new thread,it's invoked by JVM itself in runtime.though you can throw an exception for run method,how can you catch that Excetpion?yes,your code can't catch that exception too.

But class ThreadGroup have a method called uncaughtException,you can override this method to do the things you want when thread exception occured.

And as Ovi said, callback or some other ways can do your purpose.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

col_harrisAuthor Commented:
I see now why throwing an exception from a thread would never really work. Looking into the options suggested above and it looks like I will probably use an Observer and make my thread class observable.

I would also like to know how I should use a callback.. I tried doing some research on callbacks but only found irrelevant info. How do I implement a callback or where can I find some more information on them?
0
SeaCosmosCommented:
The Observer and Observable model is the implementation of Callback,i think.
0
col_harrisAuthor Commented:
Well after a bit of a struggle and some restructuring everything is working perfectly.

I've created an Observable thread and implemented Observers to catch any errors.

Thanks to all that provided thier ideas and expertiese.

0
col_harrisAuthor Commented:
It turned out to be more than just adding an Observer but this post really pointed me in the right direction.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.