Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Exceptions in Threads (& MVC)

Thread view: 
RedGrittyBrick - 08 Nov 2007 17:20 GMT
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.
Eric Sosman - 08 Nov 2007 18:30 GMT
RedGrittyBrick wrote On 11/08/07 12:20,:
> What is a good way to handle exceptions in threads?
>
[quoted text clipped - 22 lines]
> 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.

Signature

Eric.Sosman@sun.com

Daniel Pitts - 08 Nov 2007 19:24 GMT
> 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
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurr
ency-in-practice/
>

> 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.
Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

k.adamczyk - 08 Nov 2007 19:34 GMT
> What is a good way to handle exceptions in threads?

Use UncaughtExceptionHandler (Java 5+).

Greetings,
Chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.