From 8e56a519c00f77da3053738946dedbb7801a9139 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 12 May 2018 11:23:23 +0100
Subject: [PATCH v2 24/28] ProgressMonitorExecutor: log exceptions raised by
 runnables

---
 .../progress/swing/ProgressMonitorExecutor.java    | 23 ++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/org/openstreetmap/josm/gui/progress/swing/ProgressMonitorExecutor.java b/src/org/openstreetmap/josm/gui/progress/swing/ProgressMonitorExecutor.java
index 2a7f7de56..d046344f6 100644
--- a/src/org/openstreetmap/josm/gui/progress/swing/ProgressMonitorExecutor.java
+++ b/src/org/openstreetmap/josm/gui/progress/swing/ProgressMonitorExecutor.java
@@ -1,10 +1,14 @@
 // License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.gui.progress.swing;
 
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
+import org.openstreetmap.josm.tools.Logging;
 import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -36,4 +40,23 @@ public class ProgressMonitorExecutor extends ThreadPoolExecutor {
         super.execute(command);
     }
 
+    @Override
+    public void afterExecute(final Runnable r, Throwable t) {
+        // largely as proposed by JDK8 docs
+        super.afterExecute(r, t);
+        if (t == null && r instanceof Future<?>) {
+            try {
+                Object result = ((Future<?>) r).get();
+            } catch (CancellationException cancellationException) {
+                t = cancellationException;
+            } catch (ExecutionException executionException) {
+                t = executionException.getCause();
+            } catch (InterruptedException interruptedException) {
+                Thread.currentThread().interrupt(); // ignore/reset
+            }
+        }
+        if (t != null) {
+            Logging.error("Thread {0} raised {1}", Thread.currentThread().getName(), t);
+        }
+    }
 }
-- 
2.11.0

