1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  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. package org.apache.commons.launcher;
  17. /**
  18. * A class that subclasses the {@link ThreadGroup} class. This class is used
  19. * by {@link ChildMain#main(String[])} to run the target application. By using
  20. * this class, any {@link Error} other than {@link ThreadDeath} thrown by
  21. * threads created by the target application will be caught the process
  22. * terminated. By default, the JVM will only print a stack trace of the
  23. * {@link Error} and destroy the thread. However, when an uncaught
  24. * {@link Error} occurs, it normally means that the JVM has encountered a
  25. * severe problem. Hence, an orderly shutdown is a reasonable approach.
  26. * <p>
  27. * Note: not all threads created by the target application are guaranteed to
  28. * use this class. Target application's may bypass this class by creating a
  29. * thread using the {@link Thread#Thread(ThreadGroup, String)} or other similar
  30. * constructors.
  31. *
  32. * @author Patrick Luby
  33. */
  34. public class ExitOnErrorThreadGroup extends ThreadGroup {
  35. //------------------------------------------------------------ Constructors
  36. /**
  37. * Constructs a new thread group. The parent of this new group is the
  38. * thread group of the currently running thread.
  39. *
  40. * @param name the name of the new thread group
  41. */
  42. public ExitOnErrorThreadGroup(String name) {
  43. super(name);
  44. }
  45. //----------------------------------------------------------------- Methods
  46. /**
  47. * Trap any uncaught {@link Error} other than {@link ThreadDeath} and exit.
  48. *
  49. * @param t the thread that is about to exit
  50. * @param e the uncaught exception
  51. */
  52. public void uncaughtException(Thread t, Throwable e) {
  53. if (e instanceof ThreadDeath)
  54. return;
  55. Launcher.error(e);
  56. System.exit(1);
  57. }
  58. }