?
Solved

How can I throw an exception back from a thread.

Posted on 2003-03-12
7
Medium Priority
?
184 Views
Last Modified: 2010-03-31

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.
0
Comment
Question by:col_harris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 8125634
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
 
LVL 2

Accepted Solution

by:
functionpointer earned 100 total points
ID: 8125768

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

Instead of throwing an exception, you notify.
0
 

Expert Comment

by:SeaCosmos
ID: 8125825
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:col_harris
ID: 8132986
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
 

Expert Comment

by:SeaCosmos
ID: 8133080
The Observer and Observable model is the implementation of Callback,i think.
0
 

Author Comment

by:col_harris
ID: 8134408
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
 

Author Comment

by:col_harris
ID: 8134412
It turned out to be more than just adding an Observer but this post really pointed me in the right direction.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question