@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);
    }
  }
@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) {
      // Ignore any exceptions being thrown and validate that the runtime is halted below.
    }
    verify(runtime).halt(1);
  }