[英]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")
只需print
并break
,因为您将其保存在循环中
while True:
try:
x = "Hello World"
int(x)
except:
print("Error: Check Section 1.0")
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.