def die(exc: Exception = None, exit_code: int = 1) -> None:
    """
    It is not clear that Python guarantees to exit with a non-zero exit code
    (errorlevel in DOS/Windows) upon an unhandled exception. So this function
    produces the usual stack trace then dies with the specified exit code.

    See
    http://stackoverflow.com/questions/9555133/e-printstacktrace-equivalent-in-python.

    Test code:
    
    .. code-block:: python

        import logging
        import sys
        import traceback
        logging.basicConfig(level=logging.DEBUG)
        log = logging.getLogger()
        
        def fail():
            try:
                x = 1/0
            except Exception as exc:
                die(exc)

    Then call
    
    .. code-block:: python
    
        fail()
        
    ... which should exit Python; then from Linux (for example):
    
    .. code-block:: bash
    
        echo $?  # show exit code

    """  # noqa
    if exc:
        lines = traceback.format_exception(
            None,  # etype: ignored
            exc,
            exc.__traceback__)  # https://www.python.org/dev/peps/pep-3134/
        msg = "".join(lines)
        # Method 1:
        # print("".join(lines), file=sys.stderr, flush=True)
        # Method 2:
        log.critical(msg)
    log.critical("Exiting with exit code {}", exit_code)
    sys.exit(exit_code)