Throws exception into Threads

F

Fran Garcia

I´ve developped an applet that run Threads and I wanna handle the
errors throwing exceptions. I´ve readen a lot of post and they comment
that is impossible make it. But I´ve not found a good solution. Can
anybody help me?

Any suggestion would be appreciated!

Best regards
 
A

Andrea Desole

I would use a variable in the thread to let the threads communicate
possible exceptions:

class MyThread extends Thread
{
private Throwable exception = null;

public Throwable getException()
{
return exception;
}

public void run()
{
try
{
// do things
}
catch ( Throwable t )
{
exception = t;
}
}
}

public void foo()
{
MyThread t = new MyThread();

t.run();
t.join(); // or whatever is needed to know
//that the thread is terminated
t.sleep();
if ( t.exception() != null )
// error handling
}
 
S

Stefan Schulz

I´ve developped an applet that run Threads and I wanna handle the
errors throwing exceptions. I´ve readen a lot of post and they comment
that is impossible make it. But I´ve not found a good solution. Can
anybody help me?

Any suggestion would be appreciated!

Disclaimer: I am not sure i understood what you want to do.

If you do mean what i think you mean, then indeed it is not possible.
You can't "push" an Exception into another Thread of execution. All
Exceptions never leave their originating Thread.

If you want to use the Exceptions to signal the Threads that they
should stop working, you would do much better to check some
"chancel" field in the Runnable every so often, and exit the
thread if it ever becomes "true"
 
J

John C. Bollinger

Fran said:
I´ve developped an applet that run Threads and I wanna handle the
errors throwing exceptions. I´ve readen a lot of post and they comment
that is impossible make it. But I´ve not found a good solution. Can
anybody help me?

Much depends on what you mean to do with such exceptions. One
alternative is to create a subclass of ThreadGroup that overrides the
uncaughtException() method (compatibly!) so that it does something
useful to you. You then assign the threads created by your applet to an
instance of this ThreadGroup. Do note that the uncaughtException method
will be invoked by some unspecified thread in the JVM, which is probably
not the thread wherein the exception was thrown, and surely not the
applet's main thread (inasmuch as it has one). All that assumes you
mean to do something different than handle exceptions by making
appropriate use of Java's try / catch mechanism within the context of a
single thread.

If any of that fails to make sense to you, then you would be best off to
study some of the basics of programming with threads, and possibly the
basics of using exceptions. Writing correct multithreaded programs is
tricky -- easier in Java than in some languages, but tricky anywhere.
If you don't have a firm grasp of the language basics before you start
in to work with threads then you are toast. Crispy brown toast.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top