Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How can I throw an exception back from a thread.

Posted on 2003-03-12
7
Medium Priority
?
187 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

580 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