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