有没有一种简单的方法可以自定义python中的try(除了错误代码输出)



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

while True:
try:
x = "Hello World"
int(x)
except (ValueError, RuntimeError, TypeError, NameError, SystemExit):
sys.exit("Error: Check Section 1.0")

创建输出:

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

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

Error: Check Section 1.0

只需执行printbreak,因为您将其保持在循环中

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

试试这个,这个方法直接向您显示"错误:检查第1.0节";只有

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

最新更新