Dato il seguente codice:
Non riesco a capire perchè non viene gestita l'eccezione lanciata nel metodo run() della classe ExceptionThread, mentre invece funziona tutto se inserisco la linea di codice nel commento. Qualcuno sa dirmi qualcosa?codice:import java.util.*; import java.util.concurrent.*; class ExceptionThread implements Runnable { public void run() { throw new RuntimeException(); } } class Handler implements Thread.UncaughtExceptionHandler { public void uncaughtException(Thread t, Throwable e) { System.out.println("caught " + e); } } public class Test { public static void main(String[] args) { //Thread.setDefaultUncaughtExceptionHandler(new Handler()); ExecutorService exec = Executors.newCachedThreadPool(); Thread t = new Thread(new ExceptionThread()); t.setUncaughtExceptionHandler(new Handler()); exec.execute(t); } }
Grazie.

Rispondi quotando