You cannot catch child thread exceptions through try catch. The Thread object provides the setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method to obtain exceptions generated in the thread.
package threads; import java.lang.Thread.UncaughtExceptionHandler; public class TextException { public static void main(String[] args) { Test test = new Test(); test.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { System.out.println(t.getName() + " : " + e.getMessage()); // TODO } }); } public static class Test extends Thread { public Test() { } public void run() { throw new RuntimeException("just a test"); } } }
For more articles related to Java multi-threaded programming examples of catching sub-thread exceptions, please pay attention to the PHP Chinese website!