首页 > 解决方案 > 为什么 While True 循环不会被 Pycharm 上的“KeyboardInterrupt”中断?

问题描述

我想while True在下面的代码中中断我的循环,但它不起作用:

try:  
    while True:  
        print(1)    
except KeyboardInterrupt:  
    print('interrupted!')  
    exit()

我在 PyCharm 中运行这段代码。为什么不Ctrl+C触发KeyboardInterrupt

标签: pythonwhile-looppycharmtry-catchinterrupt

解决方案


你试过按Ctrl+C吗,因为这是正确的方法KeyboardInterrupt

正如@Chris 提到的,它不适用于pycharm。尝试为此使用 Ctrl+F2


推荐阅读