目录
- 异常处理
- UncaughtExceandroidptionHandler
异常处理
线程未捕获异常 UncaughtException 需要UncaughtZExceptionHandler 来进行处理
那么为什么非要用UncaughtZExceptionHandler呢?
- 主线程可以轻松捕获线程,子线程不可以
- 从下面代码可知,即使子线程抛出异常,主线程丝毫不受影响
public class ChildException implements Runnable{ public static void main(String[] args) { new Thread(new ChildException()).start(); for (int i = 0; i < 10; i++) { System.out.println(i); } } @Override public void run() { throw new RuntimeException(); } } /* * Exception in thread "Thread-0" Java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.ChildException.run(ChildException.java:14) at java.lang.Thread.run(Thread.java:748) 0 1 2 3 4 5 6 7 8 9 * */
- 从下面代码可知,即使想用catch捕获子线程异常,时没有用的
- try catch 是针对主线程的,没有办法捕获子线程的异常
public class CantCatch implements Runnable { public static void main(String[] args) throws InterruptedException { try { new Thread(new CantCatch(), "thread0").start(); Thread.sleep(300); new Thread(new CantCatch(), "thread1").start(); Thread.sleep(www.devze.com300); new Thread(new CantCatch(), "thread2").start(); Thread.sleep(300); new Thread(new CantCatch(), "thread3").start(); Thread.sleep(300); } catch (RuntimeException e) { System.out.println("catch"); } } @Override public void run() { throw new RuntimeException(); } } /* * Exception in thread "thread0" java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22) at java.lang.Thread.run(Thread.java:748) Exception in thread "thread1" java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22) at java.lang.Thread.run(Thread.java:748) Exception in thread "thread2" java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22) at java.lang.Thread.run(Thread.java:748) Exception in thread "thread3" java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22) at java.lang.Thread.run(Thread.java:748) Process finished with exit code 0 * */
在run方法中进行try c编程客栈atch可以捕获到异常,但是特别麻烦,因为需要手动地在每个run方法中都进行try catch
UncaughtExceptionHandler
自定义UncaughtExceptionHandler
public class MyUncaughtHandler implements Thread.UncaughtExceptionHandler{ private String name; public MyUncaughtHandler(String name) { this.name = name; } @Override public void uncaughtException(Thread t, Throwable e) { Logger logger = Logger.getAnonymousLogger(); logger.log(Level.WARNING, "线程异常" + t.getName(), e); System.out.println(name + "捕获" + t.getName()+ e); } }
使用自定义的类来捕获异常
public class UseownExceptionHandler implements Runnable { public static void main(String[] args) throws InterruptedException { Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtHandler("MyHandler")); // try { new Thread(new UseOwnExceptionHandler(), "thread0").start(); Thread.sleep(300); new Thread(new UseOwnExceptionHandler(), "thread1").start(); Thread.sleep(300); new Thread(new UseOwnExceptionHandler(), "thread2").start(); Thread.sleep(300); new Thread(new UseOwnExceptionHandler(), "thread3").start(); Thread.sleep(300); // } catch (RuntimeException e) { // System.out.println("catch"); // } } @Override public void run() { // try { throw new RuntimeException(); // } catch (RuntimeException e) { // php System.out.println("e"); // } } } /* * 一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtExcep开发者_Js入门tion 警告: 线程异常thread0 java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24) at java.lang.Thread.run(Thread.java:748) MyHandler捕获thread0java.lang.RuntimeException 一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException 警告: 线程异常thread1 java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24) at java.lang.Thread.run(Thread.java:748) MyHandler捕获thread1java.lang.RuntimeException 一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException 警告: 线程异常thread2 java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24) at java.lang.Thread.run(Thread.java:748) MyHandler捕获thread2java.lang.RuntimeException 一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException 警告: 线程异常thread3 java.lang.RuntimeException at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24) at java.lang.Thread.run(Thread.java:748) phpMyHandler捕获thread3java.lang.RuntimeException Process finished with exit code 0 * */
到此这篇关于Java异常处理UncaughtExceptionHandler使用实例代码详解的文章就介绍到这了,更多相关Java UncaughtExceptionHandler内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论