Friday, October 09, 2015

Exception handling in Thread and SwingWorker

Exception handling in SwingWorker differs from Thread. When using a Thread if an exception is thrown inside run(), it is propagated up the chain and you can handle it outside if you want. When an exception occurs in SwingWorker.doInBackground(), you have to handle it in the done() method. The following code demonstrates this. If you delete the done() method, it will run but the exception will be lost.
/**
 * Demonstrates difference of SwingWorker and Thread when an exception occurs. In SwingWorker, if you don't catch the exception in done(), it gets lost.
 */
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Thread.run");
            throw new IllegalArgumentException();
        }
    }).start();

    new SwingWorker() {
        @Override
        protected Object doInBackground() throws Exception {
            System.out.println("SwingWorker.doInBackground");
            if (true) {
                throw new IllegalArgumentException();
            }
            return null;
        }
        @Override
        protected void done() {
            try {
                get();
            } catch (InterruptedException e) {
                System.out.println("Caught exception: " + e);
            } catch (ExecutionException e) {
                System.out.println("Caught exception: " + e);
            }
        }
    }.execute();
}

Output:
For more information see How should I handle exceptions when using SwingWorker?

Tuesday, October 06, 2015

Less Medicine, More Health

Less Medicine, More Health was especially relevant to me because my mother (65 years old) underwent colonoscopy as part of a routine control in which the doctor removed a few polyps and one of them had cancer cells close to the colon wall. Three out of four doctors said that she must have an operation which would remove a quarter of her colon (without saying anything about the risks of the operation). Only one doctor pointed out that may be all the cancer cells were taken out during the polyp removal, the risks and probability of success of the operation (which were horrible numbers). In the end my mother reasoned that the operation was not worth it. This was 6 months ago. Today she is alive and well, tending her garden.

Almost everybody has some sort of abnormality that won't hurt him but becomes a problem due to early diagnosis, i.e. you would be better of if did not perform the checks in the first place. Lesson: Don't do full checkups, only deal with things that you have symptoms of.

It shows the importance of randomization and biases that can be overlooked when performing medical trials, for example a trial based on volunteers can be biased because health conscious individuals are healthier than the average.