UncaughtExceptions.sh revision 2362
2362N/A# Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 0N/A# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A# This code is free software; you can redistribute it and/or modify it 0N/A# under the terms of the GNU General Public License version 2 only, as 0N/A# published by the Free Software Foundation. 0N/A# This code is distributed in the hope that it will be useful, but WITHOUT 0N/A# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A# version 2 for more details (a copy is included in the LICENSE file that 0N/A# accompanied this code). 0N/A# You should have received a copy of the GNU General Public License version 0N/A# 2 along with this work; if not, write to the Free Software Foundation, 0N/A# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A# @bug 4833089 4992454 0N/A# @summary Check for proper handling of uncaught exceptions 0N/A# @author Martin Buchholz 0N/A java=
"${TESTJAVA+${TESTJAVA}/bin/}java" 0N/Ajavac=
"${TESTJAVA+${TESTJAVA}/bin/}javac" 0N/A echo "-----------------------------------------------------------------" 0N/A test "$#" = 1 -a "$1" = "Line" || Die "Usage: Bottom Line" 0N/A echo "PASS: all tests gave expected results" 0N/A test "${saveFailed}" = "${failed}" && \ 0N/A echo "PASS: command completed as expected" 0N/Aimport static java.lang.Thread.*; 0N/Aimport static java.lang.System.*; 0N/Aclass OK implements UncaughtExceptionHandler { 0N/A public void uncaughtException(Thread t, Throwable e) { 0N/Aclass NeverInvoked implements UncaughtExceptionHandler { 0N/A public void uncaughtException(Thread t, Throwable e) { 0N/A err.println("Test failure: This handler should never be invoked!"); 0N/Apublic class Seppuku extends Thread implements Runnable { 0N/A public static void seppuku() { throw new RuntimeException("Seppuku!"); } 0N/A public void run() { seppuku(); } 0N/A public static void main(String[] args) throws Exception { 0N/A#---------------------------------------------------------------- 0N/A# A thread is never alive after you've join()ed it. 0N/A#---------------------------------------------------------------- 0N/ARun 0 "OK" "Exception in thread \"Thread-0\".*Seppuku" " 0N/A Thread t = new Seppuku(); 0N/A#---------------------------------------------------------------- 0N/A# Even the main thread is mortal - here it terminates "abruptly" 0N/A#---------------------------------------------------------------- 0N/ARun 1 "OK" "Exception in thread \"main\".*Seppuku" " 0N/A final Thread mainThread = currentThread(); 0N/A new Thread() { public void run() { 0N/A catch (InterruptedException e) {} 0N/A#---------------------------------------------------------------- 0N/A# Even the main thread is mortal - here it terminates normally. 0N/A#---------------------------------------------------------------- 0N/A final Thread mainThread = currentThread(); 0N/A new Thread() { public void run() { 0N/A catch (InterruptedException e) {} 0N/A#---------------------------------------------------------------- 0N/A# Check uncaught exception handler mechanism on the main thread. 0N/A# Check that thread-level handler overrides global default handler. 0N/A#---------------------------------------------------------------- 0N/A currentThread().setUncaughtExceptionHandler(new OK()); 0N/A setDefaultUncaughtExceptionHandler(new NeverInvoked()); 0N/A setDefaultUncaughtExceptionHandler(new OK()); 0N/A#---------------------------------------------------------------- 0N/A# Check uncaught exception handler mechanism on non-main threads. 0N/A#---------------------------------------------------------------- 0N/A Thread t = new Seppuku(); 0N/A setDefaultUncaughtExceptionHandler(new OK()); 0N/A new Seppuku().start();" 0N/A#---------------------------------------------------------------- 0N/A# Test ThreadGroup based uncaught exception handler mechanism. 0N/A# Since the handler for the main thread group cannot be changed, 0N/A# there are no tests for the main thread here. 0N/A#---------------------------------------------------------------- 0N/A setDefaultUncaughtExceptionHandler(new NeverInvoked()); 0N/A new ThreadGroup(\"OK\") { 0N/A public void uncaughtException(Thread t, Throwable e) {