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) {
}
}
package com.codegym.task.task25.task2512;
/*
Charting our own course
*/
public class Solution implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
t.interrupt();
StringBuilder sb = new StringBuilder();
int n = e.getStackTrace().length;
for (int i =0; i < n; i++) {
sb.append(e.getStackTrace()[i].toString() + ": " + e.getMessage());
sb.append("\n");
}
int last = sb.lastIndexOf("\n");
sb.delete(last, sb.length());
System.out.print(sb.toString());
}
public static void main(String[] args) {
}
}