Home > Java > javaTutorial > Example of catching sub-thread exceptions in java multi-thread programming

Example of catching sub-thread exceptions in java multi-thread programming

高洛峰
Release: 2017-01-18 14:53:58
Original
1923 people have browsed it

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");
    }
  }
}
Copy after login

For more articles related to Java multi-threaded programming examples of catching sub-thread exceptions, please pay attention to the PHP Chinese website!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template