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 / GUI / February 2007

Tip: Looking for answers? Try searching our database.

Handling exceptions in SwingWorker

Thread view: 
Efi Merdler - 26 Feb 2007 16:37 GMT
Hello,
I didn't find any information on this subject.
Let's assume that I have an exception while running my background
method, what are my available options :
1. Handle the exception from within the background method, sometimes
not possible especially if it requires handling GUI.
2. Create a getter method called getException() that returns the
exception if one exists and null if none exists.
The background method can return some kind of a value indicating that
something wrong happened like null (although not always possible).
You have to make sure that the background methods terminates quietly
i.e. does not allow the exception to propagate up but to catch it.
In the done() method which is called after background method returned
you can check getException() and do what ever you think you should do
(logging, display an error message etc...)

Does the above solution sounds ok ?

Thanks,
Efi
Michael Rauscher - 27 Feb 2007 05:17 GMT
> Hello,
> I didn't find any information on this subject.
[quoted text clipped - 4 lines]
> 2. Create a getter method called getException() that returns the
> exception if one exists and null if none exists.

OK, I'd like to add some possibilities:

3. Use a global 'ErrorManager'

4. Create an 'ExceptionListener' and a method addExceptionListener

5. Use a combination of 3 and 4

Bye
Michael
Rogan Dawes - 27 Feb 2007 08:09 GMT
>> Hello,
>> I didn't find any information on this subject.
[quoted text clipped - 15 lines]
> Bye
> Michael

6. Extend SwingWorker to have both a done() and exception(Exception e)
method, both of which are called on the EDT. Depending on requirements,
you may want both methods to be called if an Exception occurs, or the
exception() method could chain to the done() method.

7. Catch the exception in your worker method, and set the return value
to the exception. In your done() method, check if you get an instance of
Exception and handle it appropriately.

Rogan
Efi Merdler - 27 Feb 2007 19:23 GMT
> >> Hello,
> >> I didn't find any information on this subject.
[quoted text clipped - 26 lines]
>
> Rogan

Great !
How do you call a method on the EDT, do you use
SwingUtilities.InvokeLater(new Runner(...
exception(e));

?

Thanks,
Efi
Rogan Dawes - 27 Feb 2007 21:05 GMT
>>>> Hello,
>>>> I didn't find any information on this subject.
[quoted text clipped - 34 lines]
> Thanks,
> Efi

I'd take a careful look at the source for SwingWorker. The concurrency
issues here are quite severe, it seems. It took 3 or more revisions of
SwingWorker by seasoned practitioners before it became robust . . .

Rogan
Michael Rauscher - 28 Feb 2007 10:23 GMT
> How do you call a method on the EDT, do you use
> SwingUtilities.InvokeLater(new Runner(...
> exception(e));

SwingUtilities.invokeLater( new Runnable() {
    public void run() {
        exception(e);
    }
});

You can also implement a method in non-EDT-safe way, too:

public void exception( final Throwable e ) {
    if ( SwingUtilities.isEventDispatchThread() ) {
        // manipulate the GUI
    } else {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                exception(e);
            }
        });
    }
}

Bye
Michael


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



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