Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5972351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:38:44+00:00 2026-05-22T20:38:44+00:00

I use SwingWorker in Java 6 to avoid running long-running code on the event

  • 0

I use SwingWorker in Java 6 to avoid running long-running code on the event dispatch thread.

If the call to get() in my done() method returns an exception, what is an appropriate way of handling the exception?

I’m particularly concerned about possible InterruptedExceptions. The JavaDoc example simply ignores the exception but I’ve learnt over the years that swallowing exceptions leads to hard-to-debug code.

A sample usage is as follows:

new SwingWorker<String, Void>() {

    @Override
    protected String doInBackground() throws Exception {
        // do long-running calculation
        return result;
    }

    @Override
    protected void done() {
        try {
            setTextField(get());
        } catch (InterruptedException e) {
            e.printStackTrace();  
        } catch (ExecutionException e) {
            e.printStackTrace();  
        }
    }
}.execute();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:38:45+00:00Added an answer on May 22, 2026 at 8:38 pm

    It’s an old post, but I want to do some clarification:

    SwingWorker.get throws InterruptedException, ExecutionException as checked exceptions.

    Plus it throws a very specific unchecked exception that is CancellationException.
    Of course it could potentially throw any unchecked exception, but CancellationException is not an “exceptional” and unexpected one. It is thrown when you try to call get method after that has been called cancel.

    ExecutedException is thrown when an Exception is thrown inside doInBackground.
    The original exception is wrapped inside an ExecutionException.
    When the get() method will be called, the ExecutionException will be thrown.
    The idea of take out the original exception and manage that is good. (As Emil H pointed out).

    CancellationException is unchecked and in my opinion should be checked.
    The only excuse for the API implementation not to have it as checked is that it has a status method isCancelled().
    You can either:
    – test isCancelled() and if is true do NOT call get() since it will throw CancellationException
    – surround get() with try-catch and add the CancellationException, that since unchecked will not requested by compiler
    – the fact that CancellationException is not checked leave you free to forget all that stuff and get a nice surprise.
    – do anything because you won’t cancel the worker

    InterruptedException. If you cancel a SwingThread with cancel(true) the first interruptable method call (for sure Thread.sleep, this.wait, maybe some IO methods) in doInBackground will throw InterruptException. But this exception is not wrapped in an ExecuteException. The doInBackground is left to terminate with interrupted exception. If it is catched and converted to some other exception, those will be ignored because by this moment cancel has already invoked SwingThread.done on the EDT, and if done has called get, it has get just the standard CancellationException. Not an InterruptedException!

    If you cancel with cancel(false) no InterruptException is raised inside doInBackground.
    The same if you cancel with cancel(true) but there are no interruptable method calls inside doInBackground.
    In these cases, the doInBackground will follow its natural loop. This loop should test the isCancelled method and exit gracefully.
    If the doInBackground doesn’t do so, it will run forever.
    I’ve not tested for the presence of timeouts, but I not belive so.

    For me, it remains just a gray area.
    In which cases is InterruptedException thrown by get? I’d like to see some short code, since I couldn’t produce a similar exception. 🙂

    P.S.
    I’ve documented in another Question&Answer that the done and state change listener in case of cancellation are called before the doInBackground exits.
    Since this is true, this -that is not a bug- requires a special attention when designing doInBackground method. If you are intrested in this, see SwingWorker: when exactly is called done method?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with SwingWorker and it's done() method. I have an application that
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
Our Swing application performs some long-running tasks in a background thread using the excellent
I have a swingworker in my java project. I use netbean profiler to monitor
SwingWorker lets you prepare some data in a background Thread and then use it
I know how to use SwingWorker threads, but I still don't have precise criteria
I have a question regarding SwingWorker and Java GUI's. I have several classes which
Use of java.net.URLConnection is asked about pretty often here, and the Oracle tutorial is
use pthread_create to create limited number of threads running concurrently Successfully compile and run
I want to be able to use SwingWorker subclass multiple times. Is this possible?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.