[Python] Pausing during debugging in VSCode makes the Python Exception uncaught, unhandled and...

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Pausing during debugging in VSCode seems to make the Python Exception uncaught.

    The following is a minimal example to reproduce my problem.

    import contextlib, signal

    @contextlib.contextmanager
    def time_limit(seconds):
    def signal_handler(signum, frame):
    raise RuntimeError("Timed out!")

    signal.setitimer(signal.ITIMER_REAL, seconds)
    signal.signal(signal.SIGALRM, signal_handler)
    try:
    yield
    finally:
    signal.setitimer(signal.ITIMER_REAL, 0)


    if __name__ == "__main__":
    print("start")
    try:
    with time_limit(10):
    a=1
    while True:
    a = -a
    except RuntimeError:
    print("timed out")
    print("finished")


    I click the pause (F6) halfway through running, and the program pauses on the line a=-a as expected. After several seconds, the time limit is reached, and the RuntimeError is thrown, but the program fails to catch the error. The trackback is printed in the console, but the program is not interrupted (still paused). As a result, the program will run forever after clicking continue (F5).

    In contrast, if you set a breakpoint at the a=-a. Run the program, and the program will stop at the breakpoint. Wait a few seconds, and the program will catch the runtime error and finish successfully. The program will continue automatically once it receives the Exception without the user's instruction, which is also strange to me.

    Continue reading...

Compartilhe esta Página