Negative Cases:
import java.util.concurrent.*;
class ExceptionThread implements Runnable {
public void run() {
throw new RuntimeException();
}
}
public class ExceptionHandler {
public static void main(String... args) {
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ExceptionThread());
}
}
Result:
$ java ExceptionHandler
Exception in thread "pool-1-thread-1" java.lang.RuntimeException
at ExceptionThread.run(ExceptionHandler.java:5)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
import java.util.concurrent.*;
class ExceptionThread implements Runnable {
public void run() {
throw new RuntimeException();
}
}
public class ExceptionHandler {
public static void main(String... args) {
try{
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ExceptionThread());
} catch(RuntimeException e){
System.out.println("Caught Exception!");
}
}
}
$ java ExceptionHandler
Exception in thread "pool-1-thread-1" java.lang.RuntimeException
at ExceptionThread.run(ExceptionHandler.java:5)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Positive Cases:
- catch exception in thread
import java.util.concurrent.*;
class ExceptionThread implements Runnable {
public void run() {
try{
throw new RuntimeException();
} catch(RuntimeException e) {
System.out.println("Caught Exception");
}
}
}
public class ExceptionHandler {
public static void main(String... args) {
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ExceptionThread());
exec.shutdown();
}
}
- handle exception with uncaughtExceptionHandler
import java.util.concurrent.*;
class ExceptionThread implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.println("run by " + t);
System.out.println("eh = " + t.getUncaughtExceptionHandler());
throw new RuntimeException();
}
}
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Caught " + e );
}
}
class HandlerThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
System.out.println(this + " creating new thread");
Thread t = new Thread(r);
t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
System.out.println("eh = " + t.getUncaughtExceptionHandler());
return t;
}
}
public class ExceptionHandler {
public static void main(String... args) {
ExecutorService exec = Executors.newCachedThreadPool(new HandlerThreadFactory());
exec.execute(new ExceptionThread());
exec.shutdown();
}
}
$ java ExceptionHandler
HandlerThreadFactory@7d4991ad creating new thread
eh = MyUncaughtExceptionHandler@28d93b30
run by Thread[Thread-0,5,main]
eh = MyUncaughtExceptionHandler@28d93b30
Caught java.lang.RuntimeException