繁体   English   中英

是否有一种简单的方法可以自定义 python 中的 try-except 错误代码输出?

[英]Is there an easy way to customize try-except error code outputs in python?

我正在尝试在我正在编写的脚本中自定义我的错误代码,我想知道我是否可以调整 output。 这是场景。 运行此脚本:

while True:
    try:
        x = "Hello World"
        int(x)
    except (ValueError, RuntimeError, TypeError, NameError, SystemExit):

        sys.exit("Error: Check Section 1.0")

创建一个 output :

An exception has occurred, use %tb to see the full traceback.
SystemExit: Error: Check Section 1.0

有没有一种简单的方法,当发生错误时,脚本停止并且只说:

Error: Check Section 1.0

试试这个,这个方法直接向您显示“错误:检查第 1.0 节”。

while True:
    try:
       x = "Hello World"
       int(x)
    except:
       sys.exit("Error: Check Section 1.0")

只需printbreak ,因为您将其保存在循环中

while True:
    try:
        x = "Hello World"
        int(x)
    except:
        print("Error: Check Section 1.0")
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM