a task ( Callable or Runnable ) sent to ThreadPoolExecutors will be converted to FuturnTask , contains a link called Callable , equal to the task you are sending. FuturnTask has its own run method as follows. All exceptions or throws thrown in c.call() will be caught and placed in a nickname named outcome . When calling the FuturnTask method get outcome will be selected
FuturnTask.run Source Code Jdk1.8
public void run() { ... try { Callable<V> c = callable; if (c != null && state == NEW) { V result; boolean ran; try { result = c.call(); ran = true; } catch (Throwable ex) { result = null; ran = false;
if you want to catch the exception:
- 1. Scuffman answer
- 2. overwrite `afterExecute` when you new ThreadPoolExecutor
@Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); Throwable cause = null; if (t == null && r instanceof Future) { try { ((Future<?>) r).get(); } catch (InterruptedException | ExecutionException e) { cause = e; } } else if (t != null) { cause = t; } if (cause != null) {
gilon chiu Aug 25 '16 at 11:49 2016-08-25 11:49
source share