public class Solution implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { t.interrupt(); Deque<Throwable> stack = new ArrayDeque<>(); Throwable ie = new Throwable(e); while (true) { ie = ie.getCause(); stack.push(ie); if (ie.getCause() == null) { //stack.push(ie); break; } } for(Throwable i : stack) { System.out.println(i); } }} public static void main(String[] args) { } }