有没有一种简单的方法可以自定义python中的“除错误代码外的尝试”输出?
我试图将我的错误代码保存在我正在编写的脚本中,我想知道是否可以调整输出。下面是一个场景。运行此脚本:有没有一种简单的方法可以自定义python中的“除错误代码外的尝试”输出?,python,python-3.x,try-except,Python,Python 3.x,Try Except,我试图将我的错误代码保存在我正在编写的脚本中,我想知道是否可以调整输出。下面是一个场景。运行此脚本: while True: try: x = "Hello World" int(x) except (ValueError, RuntimeError, TypeError, NameError, SystemExit): sys.exit("Error: Check Section 1.0"
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
请尝试此方法,此方法仅显示“错误:检查第1.0节”
只需执行
print
和break
操作,因为您将其保持在一个循环中
while True:
try:
x = "Hello World"
int(x)
except:
print("Error: Check Section 1.0")
break
这并没有解决我这边的问题:“出现异常,请使用%tb查看完整的回溯。{/n}SystemExit:错误:检查第1.0节”这有效!非常感谢。
while True:
try:
x = "Hello World"
int(x)
except:
print("Error: Check Section 1.0")
break