001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.launcher;
019
020/**
021 * A class that subclasses the {@link ThreadGroup} class. This class is used
022 * by {@link ChildMain#main(String[])} to run the target application. By using
023 * this class, any {@link Error} other than {@link ThreadDeath} thrown by
024 * threads created by the target application will be caught the process
025 * terminated. By default, the JVM will only print a stack trace of the
026 * {@link Error} and destroy the thread. However, when an uncaught
027 * {@link Error} occurs, it normally means that the JVM has encountered a
028 * severe problem. Hence, an orderly shutdown is a reasonable approach.
029 * <p>
030 * Note: not all threads created by the target application are guaranteed to
031 * use this class. Target application's may bypass this class by creating a
032 * thread using the {@link Thread#Thread(ThreadGroup, String)} or other similar
033 * constructors.
034 *
035 * @author Patrick Luby
036 */
037public class ExitOnErrorThreadGroup extends ThreadGroup {
038
039    //------------------------------------------------------------ Constructors
040
041    /**
042     * Constructs a new thread group. The parent of this new group is the
043     * thread group of the currently running thread.
044     *
045     * @param name the name of the new thread group
046     */
047    public ExitOnErrorThreadGroup(String name) {
048
049        super(name);
050
051    }
052
053    //----------------------------------------------------------------- Methods
054
055    /**
056     * Trap any uncaught {@link Error} other than {@link ThreadDeath} and exit.
057     *
058     * @param t the thread that is about to exit
059     * @param e the uncaught exception
060     */
061    public void uncaughtException(Thread t, Throwable e) {
062
063        if (e instanceof ThreadDeath)
064            return;
065
066        Launcher.error(e);
067        System.exit(1);
068
069    }
070
071}