python - 停止点击(python 模块)捕捉/处理 KeyboardInterrupt

标签 python python-3.x error-handling

我有以下代码:

import click


@click.command()
def main():
    while True:
        pass


try:
    main()
except KeyboardInterrupt:
    print('My own message!')

当我按 Ctrl+C 退出程序时,我想打印我自己的消息。然而,点击截获错误,这是输出:

^C
Aborted!

如何阻止点击处理错误?

最佳答案

我想我已经用这段代码解决了我的问题!希望这是处理我的问题的正确方法。

import click


@click.command()
def main():
    while True:
        pass


try:
    main(standalone_mode=False)
except click.exceptions.Abort:
    print('My own message!')

关于python - 停止点击(python 模块)捕捉/处理 KeyboardInterrupt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61163266/

相关文章:

python - Gimp,python-fu : RuntimeError: execution error in pdb. gimp_image_merge_down

django - 如何使用命令 CMD 从 Windows 7 上的 python django 访问和配置 postgresql 数据库

python - 使用一个递归调用实现递归

string - 如何将 to_string() 功能添加到枚举中?

python - 使用 Python 3 关闭/终止 Web 浏览器?

python - linspace 总是包含最后一点?

python - 日期时间差异(以小时为单位),不包括周末

c++ - 以下代码中的段错误

emacs - Elisp : possible to automatically open Emacs init file if error at start-up?

Python:从路径递归创建字典