Catch exceptions in thread java

Java Concurrent multithreading exception handling

2. Two strategies of multithreading exception handling

2-1. Try catch manually in the child thread (not recommended)

public class ThreadException implements Runnable @Override public void run()  try  throw new RuntimeException("Run err. "); > catch (Exception e)  System.out.println("Thread err warning. "); > > public static void main(String[] args) throws InterruptedException  new Thread(new ThreadException()).start(); Thread.sleep(1000); new Thread(new ThreadException()).start(); Thread.sleep(1000); > > // Operation result Thread err warning.... Thread err warning.... 

2-2. UncaughtExceptionHandler exception handling provided in thread class

This exception handler can be designed for different dimensions

  • Design an exception handler for the program
  • Each thread is designed separately
  • Design a line pool
// Define an exception handler public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler  private String handlerName; public MyUncaughtExceptionHandler(String handlerName)  this.handlerName = handlerName; > @Override public void uncaughtException(Thread t, Throwable e)  System.out.println("Exception caught, processor:" + this.handlerName); System.out.println("Abnormal information: threadName:" + t.getName() + " e:" + e); > > // Use exception handler public class ThreadException implements Runnable @Override public void run()  throw new RuntimeException("Run err. "); > public static void main(String[] args) throws InterruptedException  Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("Exception handler A")); new Thread(new ThreadException()).start(); > > // Operation result //Exception caught, processor:Exception handler A //Abnormal information: threadName:Thread-0 e:java.lang.RuntimeException: Run err... 

Источник

Java Concurrent multithreading exception handling

2. Two strategies of multithreading exception handling

2-1. Try catch manually in the child thread (not recommended)

public class ThreadException implements Runnable @Override public void run()  try  throw new RuntimeException("Run err. "); > catch (Exception e)  System.out.println("Thread err warning. "); > > public static void main(String[] args) throws InterruptedException  new Thread(new ThreadException()).start(); Thread.sleep(1000); new Thread(new ThreadException()).start(); Thread.sleep(1000); > > // Operation result Thread err warning.... Thread err warning.... 

2-2. UncaughtExceptionHandler exception handling provided in thread class

This exception handler can be designed for different dimensions

  • Design an exception handler for the program
  • Each thread is designed separately
  • Design a line pool
// Define an exception handler public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler  private String handlerName; public MyUncaughtExceptionHandler(String handlerName)  this.handlerName = handlerName; > @Override public void uncaughtException(Thread t, Throwable e)  System.out.println("Exception caught, processor:" + this.handlerName); System.out.println("Abnormal information: threadName:" + t.getName() + " e:" + e); > > // Use exception handler public class ThreadException implements Runnable @Override public void run()  throw new RuntimeException("Run err. "); > public static void main(String[] args) throws InterruptedException  Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("Exception handler A")); new Thread(new ThreadException()).start(); > > // Operation result //Exception caught, processor:Exception handler A //Abnormal information: threadName:Thread-0 e:java.lang.RuntimeException: Run err... 

Источник

Читайте также:  Java exception message set
Оцените статью