Sub-thread exceptions cannot be caught through try catch. The Thread object provides the setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method to obtain exceptions generated in the thread.
Copy the code code as follows:
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
{
publicTest()
{
}
public void run()
{
throw new RuntimeException("just a test");
}
}
}