1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * Test the uncaught exception handler. 19 */ 20 public class Main { main(String[] args)21 public static void main(String[] args) { 22 testThread(1); 23 testThread(2); 24 testThread(3); 25 26 catchTheUncaught(1); 27 } 28 testThread(int which)29 private static void testThread(int which) { 30 Thread t = new Helper(which); 31 t.start(); 32 33 try { 34 t.join(); 35 } catch (InterruptedException ex) { 36 ex.printStackTrace(); 37 } 38 } 39 catchTheUncaught(int which)40 static void catchTheUncaught(int which) { 41 ThreadDeathHandler defHandler = new ThreadDeathHandler("DEFAULT"); 42 ThreadDeathHandler threadHandler = new ThreadDeathHandler("THREAD"); 43 44 System.out.println("Test " + which); 45 switch (which) { 46 case 1: { 47 Thread.setDefaultUncaughtExceptionHandler(defHandler); 48 break; 49 } 50 case 2: { 51 Thread.currentThread().setUncaughtExceptionHandler( 52 threadHandler); 53 break; 54 } 55 case 3: { 56 Thread.setDefaultUncaughtExceptionHandler(defHandler); 57 Thread.currentThread().setUncaughtExceptionHandler( 58 threadHandler); 59 break; 60 } 61 } 62 63 throw new NullPointerException("Hi diddly-ho, neighborino."); 64 } 65 66 private static class Helper extends Thread { 67 private int which; 68 Helper(int which)69 public Helper(int which) { 70 this.which = which; 71 } 72 run()73 public void run() { 74 catchTheUncaught(which); 75 } 76 } 77 } 78