您的位置:首页 > 其它

关于多线程未捕获异常的处理介绍

2017-11-29 21:41 369 查看
 XML Code 
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

package com.lyzx.restdy.callable;

import java.util.concurrent.ThreadFactory;

public class T4 {

    public static void main(String[] args) {

        ex();

    }

    

    public static void ex(){

        try {

            new Thread(new ExceptionRunnable()).start();

        }catch(Exception e) {

            System.out.println("捕获异常......");

        }

    }

}

class ExceptionRunnable implements Runnable{

    @Override

    public void run() {

        throw new RuntimeException("抛异常了....");

    }

}
运行如下代码时发现手动抛出的异常并未被捕获,这就是java中多线程run方法中抛出的异常外部的catch是捕获不到的
运行结果如下

 XML Code 
1

2

3

Exception in thread "Thread-0" java.lang.RuntimeException: 抛异常了....

    at com.lyzx.restdy.callable.ExceptionRunnable.run(T4.java:23)

    at java.lang.Thread.run(Unknown Source)
java为捕获异常的处理

 XML Code 
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

package com.lyzx.restdy.callable;

import java.util.concurrent.ThreadFactory;

import java.util.concurrent.TimeUnit;

public class T4 {

    public static void main(String[] args) {

        //ex();

        catchEx();

        

        try {

            TimeUnit.SECONDS.sleep(10);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

    

    public static void ex(){

        try {

            new Thread(new ExceptionRunnable()).start();

        }catch(Exception e) {

            System.out.println("捕获异常......");

        }

    }

    

    public static void catchEx(){

        MyFactory f = new MyFactory();

        ExceptionRunnable r = new ExceptionRunnable();

        Thread t = f.newThread(r);

        System.out.println("catchEx:(任务)>>"+r);

        System.out.println("catchEx:(线程)>>"+t);

        t.start();

    }

}

/**

 * 在线程中抛出异常

 */

class ExceptionRunnable implements Runnable{

    @Override

    public void run() {

        RuntimeException e = new RuntimeException("抛异常了....");

        System.out.println("run:(异常)>>"+e);

        throw e;

    }

}

/**

 * 线程工厂

 * 为每一个从该工厂中生产的线程分配一个异常处理器

 */

class MyFactory implements ThreadFactory{

    @Override

    public Thread newThread(Runnable r){

        Thread t = new Thread(r);

        t.setUncaughtExceptionHandler(new MyExceptionHandler());

        return t;

    }

}

/**

 * 异常处理器类

 */

class MyExceptionHandler implements Thread.UncaughtExceptionHandler{

    /**

     * Thread t     抛异常的线程

     * Throwable e  t这个线程抛出的 异常

     */

    @Override

    public void uncaughtException(Thread t,Throwable e) {

        System.out.println(t.getName()+"被捕获,现在要做一些异常处理");

        System.out.println("uncaughtException:(异常)>>"+e);

        System.out.println("uncaughtException:(线程)>>"+t);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: