[英]Is there an easy way to customize try-except error code outputs in python?
I am trying to custimize my error codes in a script that I am writing and I was wondering if I can adjust the output.我正在尝试在我正在编写的脚本中自定义我的错误代码,我想知道我是否可以调整 output。 Here is the scenario.
这是场景。 Running this script:
运行此脚本:
while True:
try:
x = "Hello World"
int(x)
except (ValueError, RuntimeError, TypeError, NameError, SystemExit):
sys.exit("Error: Check Section 1.0")
Creates an output of:创建一个 output :
An exception has occurred, use %tb to see the full traceback.
SystemExit: Error: Check Section 1.0
Is there an easy way, when an error occurs, the script stops and only says:有没有一种简单的方法,当发生错误时,脚本停止并且只说:
Error: Check Section 1.0
Try this instead, this method direct shows you "Error: Check Section 1.0" only.试试这个,这个方法直接向您显示“错误:检查第 1.0 节”。
while True:
try:
x = "Hello World"
int(x)
except:
sys.exit("Error: Check Section 1.0")
Just do print
and do break
since you keep it inside a loop只需
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.