Coverage Summary for Class: UncaughtExceptionHandlers (com.google.common.util.concurrent)

Class Method, % Line, %
UncaughtExceptionHandlers 0% (0/2) 0% (0/2)
UncaughtExceptionHandlers$Exiter 0% (0/3) 0% (0/10)
Total 0% (0/5) 0% (0/12)


1 /* 2  * Copyright (C) 2010 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.util.concurrent; 16  17 import static java.util.logging.Level.SEVERE; 18  19 import com.google.common.annotations.GwtIncompatible; 20 import com.google.common.annotations.VisibleForTesting; 21 import java.lang.Thread.UncaughtExceptionHandler; 22 import java.util.Locale; 23 import java.util.logging.Logger; 24  25 /** 26  * Factories for {@link UncaughtExceptionHandler} instances. 27  * 28  * @author Gregory Kick 29  * @since 8.0 30  */ 31 @GwtIncompatible 32 @ElementTypesAreNonnullByDefault 33 public final class UncaughtExceptionHandlers { 34  private UncaughtExceptionHandlers() {} 35  36  /** 37  * Returns an exception handler that exits the system. This is particularly useful for the main 38  * thread, which may start up other, non-daemon threads, but fail to fully initialize the 39  * application successfully. 40  * 41  * <p>Example usage: 42  * 43  * <pre> 44  * public static void main(String[] args) { 45  * Thread.currentThread().setUncaughtExceptionHandler(UncaughtExceptionHandlers.systemExit()); 46  * ... 47  * </pre> 48  * 49  * <p>The returned handler logs any exception at severity {@code SEVERE} and then shuts down the 50  * process with an exit status of 1, indicating abnormal termination. 51  */ 52  public static UncaughtExceptionHandler systemExit() { 53  return new Exiter(Runtime.getRuntime()); 54  } 55  56  @VisibleForTesting 57  static final class Exiter implements UncaughtExceptionHandler { 58  private static final Logger logger = Logger.getLogger(Exiter.class.getName()); 59  60  private final Runtime runtime; 61  62  Exiter(Runtime runtime) { 63  this.runtime = runtime; 64  } 65  66  @Override 67  public void uncaughtException(Thread t, Throwable e) { 68  try { 69  logger.log( 70  SEVERE, String.format(Locale.ROOT, "Caught an exception in %s. Shutting down.", t), e); 71  } catch (Throwable errorInLogging) { 72  // If logging fails, e.g. due to missing memory, at least try to log the 73  // message and the cause for the failed logging. 74  System.err.println(e.getMessage()); 75  System.err.println(errorInLogging.getMessage()); 76  } finally { 77  runtime.exit(1); 78  } 79  } 80  } 81 }