Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's an old post, but I want to do some clarification:</p> <p>SwingWorker.get throws InterruptedException, ExecutionException as checked exceptions.</p> <p>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.</p> <p>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).</p> <p>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().<br> You can either: <br> - test isCancelled() and if is true do NOT call get() since it will throw CancellationException<br> - surround get() with try-catch and add the CancellationException, that since unchecked will not requested by compiler <br> - the fact that CancellationException is not checked leave you free to forget all that stuff and get a nice surprise. <br> - do anything because you won't cancel the worker <br></p> <p>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. <em>Not an InterruptedException!</em></p> <p>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.</p> <p>For me, it remains just a <strong>gray area</strong>. 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> <p>P.S. I've documented in another Question&amp;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 <a href="https://stackoverflow.com/questions/6204141/swingworker-when-exactly-is-called-done-method">SwingWorker: when exactly is called done method?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload