Hello,
I've created some test classes that creates an array of threads. In
some cases, a thread encounters an Exception and I'm not sure how to
report this exception back to the user. In my runnable class, I have a
setError() and getError() methods but i'm can't seem to access them
from the calling class. I tried using uncaughtException, but i haven't
been able to get it to work. Does know how to capture the exception?
John B. Matthews - 30 Jun 2009 16:10 GMT
In article
<ee1c0b8f-0f41-4f7a-a88b-ae71ea190752@y9g2000yqg.googlegroups.com>,
> I've created some test classes that creates an array of threads. In
> some cases, a thread encounters an Exception and I'm not sure how to
[quoted text clipped - 3 lines]
> haven't been able to get it to work. Does know how to capture the
> exception?
You may have to synchronize access to the error:
<http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html>
You may be able to arrange for the error to be volatile or a final
reference to an immutable object.
<http://java.sun.com/docs/books/tutorial/essential/concurrency/imstrat.html>
The simplest approach may be a continuation that invokes
EventQueue.invokeLater(), which can safely update a Swing component:
<http://en.wikipedia.org/wiki/Continuation_passing_style#Continuations_as_objects>
You may want to consider other approaches mentioned here:
<http://sites.google.com/site/drjohnbmatthews/threadwatch>

Signature
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Lew - 30 Jun 2009 16:11 GMT
> I've created some test classes that creates an array of threads. In
> some cases, a thread encounters an Exception and I'm not sure how to
> report this exception back to the user. In my runnable class, I have a
> setError() and getError() methods but i'm can't seem to access them
> from the calling class. I tried using uncaughtException, but i haven't
> been able to get it to work. Does know how to capture the exception?
There are a few ways.
<http://java.sun.com/javase/6/docs/api/java/lang/
Thread.html#setDefaultUncaughtExceptionHandler
(java.lang.Thread.UncaughtExceptionHandler)>
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/
Executor.html>
<http://java.sun.com/javase/6/docs/api/java/util/concurrent/
Future.html#get()>
One of the best ways is to write the task to catch its own exception
and set some state visible to the calling thread.
Brian Goetz's seminal /Java Concurrency in Practice/ covers these
techniques in detail.
<http://jcip.net/>
--
Lew
Knute Johnson - 30 Jun 2009 17:43 GMT
> Hello,
>
[quoted text clipped - 4 lines]
> from the calling class. I tried using uncaughtException, but i haven't
> been able to get it to work. Does know how to capture the exception?
There are a bunch of ways to do this depending on what you really want
to achieve. To display a GUI message to the user for the occasional
error, you can use a simple dialog with your error message. If there
will be lots of errors or just messages to the user from these threads,
the threads can post a message to a queue. The main thread can then
retrieve the messages and display them to the user. Another option is
to look at the Future interface, it has methods to control (eg. check
completion, wait for completion and cancel) an asynchronous task.
If you want some code examples you need to be a little more specific
about what you really want to do in your program.

Signature
Knute Johnson
email s/nospam/knute2009/
markspace - 30 Jun 2009 18:19 GMT
> Hello,
>
[quoted text clipped - 4 lines]
> from the calling class. I tried using uncaughtException, but i haven't
> been able to get it to work. Does know how to capture the exception?
What everyone else has said is very true. There's many ways of doing
this, and it all depends on your goals. A code example from you would
be helpful here, as would a bit more explanation of what you're trying
to do.
For example, WHY can't you access setError() and getError()? What's the
impediment? What code do you have now and why is it difficult? Do you
have a framework that you need to use that makes it hard to use
subclasses? Or do you just not understand how to make methods visible?
Eric's suggestion is probably the easiest--just make a queue and have
your Runnables put their errors there. But maybe your goals are different.
A short compilable example (or one that shows the compile error if
that's the problem) would be most helpful to everyone here if you want
us to help you.
Lothar Kimmeringer - 30 Jun 2009 22:25 GMT
> In my runnable class, I have a
> setError() and getError() methods but i'm can't seem to access them
> from the calling class. I tried using uncaughtException, but i haven't
> been able to get it to work. Does know how to capture the exception?
One of the many ways is using a callback-function:
public class ThreadCreator{
public void threadHadException(Throwable t, MyThread mt){
//whatever
}
public final static void main(String[] args){
for (int i = 0; i < MAXNR; i++){
MyThread mt = new MyThread(this);
mt.start();
}
}
}
public class MyThread extends Thread{
private ThreadCreator creator;
public MyThread(ThreadCreator creator){
this.creator = creator;
}
public void run(){
try{
//whatever
}
catch(ThreadDeath td){
throw td;
}
catch(Throwable t){
creator.threadHadException(t, this);
}
}
}
What is the best solution for you problem depends on the
problem you want to solve.
Regards, Lothar

Signature
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
Ian Shef - 01 Jul 2009 19:25 GMT
<snip>
> public final static void main(String[] args){
> for (int i = 0; i < MAXNR; i++){
> MyThread mt = new MyThread(this);
> mt.start();
> }
> }
<snip>
I think that I see what you were trying to demonstrate, but it is obscured by
two errors in the above code:
Trivial: MAXNR is not defined.
More serious: 'this' cannot be used in a static context (main is static).