Java异常处理UncaughtExceptionHandler如何使用

其他教程   发布日期:2023年07月19日   浏览次数:519

本篇内容主要讲解“Java异常处理UncaughtExceptionHandler如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java异常处理UncaughtExceptionHandler如何使用”吧!

异常处理

线程未捕获异常 UncaughtException 需要UncaughtZExceptionHandler 来进行处理

那么为什么非要用UncaughtZExceptionHandler呢?

  • 主线程可以轻松捕获线程,子线程不可以

  • 从下面代码可知,即使子线程抛出异常,主线程丝毫不受影响

  1. public class ChildException implements Runnable{
  2. public static void main(String[] args) {
  3. new Thread(new ChildException()).start();
  4. for (int i = 0; i < 10; i++) {
  5. System.out.println(i);
  6. }
  7. }
  8. @Override
  9. public void run() {
  10. throw new RuntimeException();
  11. }
  12. }
  13. /*
  14. * Exception in thread "Thread-0" java.lang.RuntimeException
  15. at com.jx.JavaTest.Thread.Exception.ChildException.run(ChildException.java:14)
  16. at java.lang.Thread.run(Thread.java:748)
  17. 0
  18. 1
  19. 2
  20. 3
  21. 4
  22. 5
  23. 6
  24. 7
  25. 8
  26. 9
  27. * */
  • 从下面代码可知,即使想用catch捕获子线程异常,时没有用的

  • try catch 是针对主线程的,没有办法捕获子线程的异常

  1. public class CantCatch implements Runnable {
  2. public static void main(String[] args) throws InterruptedException {
  3. try {
  4. new Thread(new CantCatch(), "thread0").start();
  5. Thread.sleep(300);
  6. new Thread(new CantCatch(), "thread1").start();
  7. Thread.sleep(300);
  8. new Thread(new CantCatch(), "thread2").start();
  9. Thread.sleep(300);
  10. new Thread(new CantCatch(), "thread3").start();
  11. Thread.sleep(300);
  12. } catch (RuntimeException e) {
  13. System.out.println("catch");
  14. }
  15. }
  16. @Override
  17. public void run() {
  18. throw new RuntimeException();
  19. }
  20. }
  21. /*
  22. * Exception in thread "thread0" java.lang.RuntimeException
  23. at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
  24. at java.lang.Thread.run(Thread.java:748)
  25. Exception in thread "thread1" java.lang.RuntimeException
  26. at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
  27. at java.lang.Thread.run(Thread.java:748)
  28. Exception in thread "thread2" java.lang.RuntimeException
  29. at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
  30. at java.lang.Thread.run(Thread.java:748)
  31. Exception in thread "thread3" java.lang.RuntimeException
  32. at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
  33. at java.lang.Thread.run(Thread.java:748)
  34. Process finished with exit code 0
  35. * */

在run方法中进行try catch可以捕获到异常,但是特别麻烦,因为需要手动地在每个run方法中都进行try catch

UncaughtExceptionHandler

自定义UncaughtExceptionHandler

  1. public class MyUncaughtHandler implements Thread.UncaughtExceptionHandler{
  2. private String name;
  3. public MyUncaughtHandler(String name) {
  4. this.name = name;
  5. }
  6. @Override
  7. public void uncaughtException(Thread t, Throwable e) {
  8. Logger logger = Logger.getAnonymousLogger();
  9. logger.log(Level.WARNING, "线程异常" + t.getName(), e);
  10. System.out.println(name + "捕获" + t.getName()+ e);
  11. }
  12. }

使用自定义的类来捕获异常

  1. public class UseOwnExceptionHandler implements Runnable {
  2. public static void main(String[] args) throws InterruptedException {
  3. Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtHandler("MyHandler"));
  4. // try {
  5. new Thread(new UseOwnExceptionHandler(), "thread0").start();
  6. Thread.sleep(300);
  7. new Thread(new UseOwnExceptionHandler(), "thread1").start();
  8. Thread.sleep(300);
  9. new Thread(new UseOwnExceptionHandler(), "thread2").start();
  10. Thread.sleep(300);
  11. new Thread(new UseOwnExceptionHandler(), "thread3").start();
  12. Thread.sleep(300);
  13. // } catch (RuntimeException e) {
  14. // System.out.println("catch");
  15. // }
  16. }
  17. @Override
  18. public void run() {
  19. // try {
  20. throw new RuntimeException();
  21. // } catch (RuntimeException e) {
  22. // System.out.println("e");
  23. // }
  24. }
  25. }
  26. /*
  27. * 一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
  28. 警告: 线程异常thread0
  29. java.lang.RuntimeException
  30. at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
  31. at java.lang.Thread.run(Thread.java:748)
  32. MyHandler捕获thread0java.lang.RuntimeException
  33. 一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
  34. 警告: 线程异常thread1
  35. java.lang.RuntimeException
  36. at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
  37. at java.lang.Thread.run(Thread.java:748)
  38. MyHandler捕获thread1java.lang.RuntimeException
  39. 一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
  40. 警告: 线程异常thread2
  41. java.lang.RuntimeException
  42. at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
  43. at java.lang.Thread.run(Thread.java:748)
  44. MyHandler捕获thread2java.lang.RuntimeException
  45. 一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
  46. 警告: 线程异常thread3
  47. java.lang.RuntimeException
  48. at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
  49. at java.lang.Thread.run(Thread.java:748)
  50. MyHandler捕获thread3java.lang.RuntimeException
  51. Process finished with exit code 0
  52. * */

以上就是Java异常处理UncaughtExceptionHandler如何使用的详细内容,更多关于Java异常处理UncaughtExceptionHandler如何使用的资料请关注九品源码其它相关文章!