WorkerUncaughtExceptionHandler implements UncaughtExceptionHandler { @Override public void uncaughtException(Thread thread, Throwable e) { try { logger.error("Uncaught exception in main thread. Exiting with status code 1.", e); System.err.println("Uncaught exception in main thread. Exiting with status code 1."); e.printStackTrace(); } catch (Throwable t) { PrintStream originalStdErr = DataflowWorkerLoggingInitializer.getOriginalStdErr(); if (originalStdErr != null) { originalStdErr.println("Uncaught exception in main thread. Exiting with status code 1."); e.printStackTrace(originalStdErr); originalStdErr.println( "UncaughtExceptionHandler caused another exception to be thrown, as follows:"); t.printStackTrace(originalStdErr); } } finally { runtime.halt(1); } } WorkerUncaughtExceptionHandler(Logger logger); @VisibleForTesting  WorkerUncaughtExceptionHandler(JvmRuntime runtime, Logger logger); @Override void uncaughtException(Thread thread, Throwable e);  }
@Test public void testUncaughtExceptionHandlerForciblyHaltsRuntime() { JvmRuntime runtime = mock(JvmRuntime.class); WorkerUncaughtExceptionHandler handler = new WorkerUncaughtExceptionHandler(runtime, LOG); try { handler.uncaughtException(Thread.currentThread(), new Exception("oh noes!")); } catch (Exception e) { } verify(runtime).halt(1); }