Implement UncaughtExceptionHandler
Set it when initializing Thread
public class ExceptionHandler implements UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { System.out.printf("An exception has been captured\n"); System.out.printf("Thread: %s\n",t.getId()); System.out.printf("Exception: %s: %s\n",e.getClass().getName(),e.getMessage()); System.out.printf("Stack Trace: \n"); e.printStackTrace(System.out); System.out.printf("Thread status: %s\n",t.getState()); } }
Set it when initializing Thread
public static void main(String[] args) { // Creates the Task Task task=new Task(); // Creates the Thread Thread thread=new Thread(task); // Sets the uncaught exception handler thread.setUncaughtExceptionHandler(new ExceptionHandler()); // Starts the Thread thread.start(); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Thread has finished\n"); }
No comments:
Post a Comment