1// Copyright 2006 The Android Open Source Project
2
3/**
4 * Test the uncaught exception handler.
5 */
6public class Main {
7    public static void main(String[] args) {
8        testThread(1);
9        testThread(2);
10        testThread(3);
11
12        catchTheUncaught(1);
13    }
14
15    private static void testThread(int which) {
16        Thread t = new Helper(which);
17        t.start();
18
19        try {
20            t.join();
21        } catch (InterruptedException ex) {
22            ex.printStackTrace();
23        }
24    }
25
26    static void catchTheUncaught(int which) {
27        ThreadDeathHandler defHandler = new ThreadDeathHandler("DEFAULT");
28        ThreadDeathHandler threadHandler = new ThreadDeathHandler("THREAD");
29
30        System.out.println("Test " + which);
31        switch (which) {
32            case 1: {
33                Thread.setDefaultUncaughtExceptionHandler(defHandler);
34                break;
35            }
36            case 2: {
37                Thread.currentThread().setUncaughtExceptionHandler(
38                        threadHandler);
39                break;
40            }
41            case 3: {
42                Thread.setDefaultUncaughtExceptionHandler(defHandler);
43                Thread.currentThread().setUncaughtExceptionHandler(
44                        threadHandler);
45                break;
46            }
47        }
48
49        throw new NullPointerException("Hi diddly-ho, neighborino.");
50    }
51
52    private static class Helper extends Thread {
53        private int which;
54
55        public Helper(int which) {
56            this.which = which;
57        }
58
59        public void run() {
60            catchTheUncaught(which);
61        }
62    }
63}
64