开发者社区 问答 正文

python如何指定异常?

python如何指定异常?

展开
收起
真的很搞笑 2021-12-03 22:35:08 384 分享 版权
1 条回答
写回答
取消 提交回答
  • 可以指定要在 except 块中处理哪个错误

    try: # some code except ValueError: # some code 现在它会捕获 ValueError 异常,但是不会捕获其他异常。如果我们希望该处理程序处理多种异常,我们可以在 except后面添加异常元组。

    try: # some code except (ValueError, KeyboardInterrupt): # some code 或者,如果我们希望根据异常执行不同的代码块,可以添加多个 except 块。

    try: # some code except ValueError: # some code except KeyboardInterrupt: # some code

    2021-12-03 22:35:47
    赞同 展开评论
问答分类:
问答标签:
问答地址: