Exceptions in Threads (& MVC)

R

RedGrittyBrick

What is a good way to handle exceptions in threads?


Lets say I have an application FooApp with classes FooView and FooModel.
FooView is concerned with showing info to the user, including error
messages. FooModel is concerned with data structures, including
persistent storage and retrieval.

My FooView instance asks a FooModel instance to provide data to be
displayed. Lets say FooView instantiates FooModel. If the FooModel
instance has a problem retrieving the data from storage, it can throw an
exception. The FooView instance catches the exception thrown by 'new
FooModel()' (or e.g. by 'fooModel.retrieveFromStorage()') and then shows
an error message to the user.

However FooModel's retrieval of the data is slow, I don't want to block
the EDT. So inside FooModel the retrieval is carried out in a newly
created Thread. Now it can't throw exceptions to FooView :-(

FooView passes FooModel an Observable that FooModel modifies when it has
completed retrieving the data. FooView implements Observer and so
FooView's update() method is called when FooModel updates the observable
to say "ready". In update() FooView invokes FooModel's getXXX() methods
and uses the values to fill in the blanks in the GUI.

Should I use this Observable (or perhaps a separate Observable) to alert
FooView when FooModel catches an Exception?

Or something else?



P.S. FooModel catches SQLExceptions but throws FooExceptions since
FooView wants to be storage agnostic.

P.P.S. is buying "Java Concurrency in Practice" overkill for this?

P.P.P.S I've Googled, I've skimmed
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
and my various Java books.
 
E

Eric Sosman

RedGrittyBrick wrote On 11/08/07 12:20,:
What is a good way to handle exceptions in threads?


Lets say I have an application FooApp with classes FooView and FooModel.
FooView is concerned with showing info to the user, including error
messages. FooModel is concerned with data structures, including
persistent storage and retrieval.

My FooView instance asks a FooModel instance to provide data to be
displayed. Lets say FooView instantiates FooModel. If the FooModel
instance has a problem retrieving the data from storage, it can throw an
exception. The FooView instance catches the exception thrown by 'new
FooModel()' (or e.g. by 'fooModel.retrieveFromStorage()') and then shows
an error message to the user.

However FooModel's retrieval of the data is slow, I don't want to block
the EDT. So inside FooModel the retrieval is carried out in a newly
created Thread. Now it can't throw exceptions to FooView :-(

FooView passes FooModel an Observable that FooModel modifies when it has
completed retrieving the data. FooView implements Observer and so
FooView's update() method is called when FooModel updates the observable
to say "ready". In update() FooView invokes FooModel's getXXX() methods
and uses the values to fill in the blanks in the GUI.

Should I use this Observable (or perhaps a separate Observable) to alert
FooView when FooModel catches an Exception?

You could catch the exception in the thread where it's
thrown, and store a reference to the exception object in an
Exception variable. This could be part of the data in the
Observable: the FooView would use getException() to retrieve
it (or null if the thread finished normally) and display
information about it to the user. Or even re-throw the
Exception in its own context, possibly wrapped in another.
 
D

Daniel Pitts

RedGrittyBrick said:
What is a good way to handle exceptions in threads?
The same way you handle exceptions anywhere else. Catch the ones you
know how to handle, and let propagate the ones you don't know how to
handle.

P.S. FooModel catches SQLExceptions but throws FooExceptions since
FooView wants to be storage agnostic.
Thats good practice. Ofcourse, the Runnable.run() method should catch
the FooException and then call EventQueue.invokeLater() to call
fooView.presentExceptionToUser(caughtException);
P.P.S. is buying "Java Concurrency in Practice" overkill for this?
Maybe for this particular problem, but definitely a worth while
purchase. See my review at
P.P.P.S I've Googled, I've skimmed
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
and my various Java books.
All good starts. Just because you're dealing with threads, doesn't mean
your design should change. You might have to do a little bit of
footwork to move the exception across threads, but my suggestion about
should help :)

Oh, and look into the SwingWorker class!

Good luck,
Daniel.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top