View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.launcher;
19  
20  /**
21   * A class that subclasses the {@link ThreadGroup} class. This class is used
22   * by {@link ChildMain#main(String[])} to run the target application. By using
23   * this class, any {@link Error} other than {@link ThreadDeath} thrown by
24   * threads created by the target application will be caught the process
25   * terminated. By default, the JVM will only print a stack trace of the
26   * {@link Error} and destroy the thread. However, when an uncaught
27   * {@link Error} occurs, it normally means that the JVM has encountered a
28   * severe problem. Hence, an orderly shutdown is a reasonable approach.
29   * <p>
30   * Note: not all threads created by the target application are guaranteed to
31   * use this class. Target application's may bypass this class by creating a
32   * thread using the {@link Thread#Thread(ThreadGroup, String)} or other similar
33   * constructors.
34   *
35   * @author Patrick Luby
36   */
37  public class ExitOnErrorThreadGroup extends ThreadGroup {
38  
39      //------------------------------------------------------------ Constructors
40  
41      /**
42       * Constructs a new thread group. The parent of this new group is the
43       * thread group of the currently running thread.
44       *
45       * @param name the name of the new thread group
46       */
47      public ExitOnErrorThreadGroup(String name) {
48  
49          super(name);
50  
51      }
52  
53      //----------------------------------------------------------------- Methods
54  
55      /**
56       * Trap any uncaught {@link Error} other than {@link ThreadDeath} and exit.
57       *
58       * @param t the thread that is about to exit
59       * @param e the uncaught exception
60       */
61      public void uncaughtException(Thread t, Throwable e) {
62  
63          if (e instanceof ThreadDeath)
64              return;
65  
66          Launcher.error(e);
67          System.exit(1);
68  
69      }
70  
71  }