[英]How do I have a python function return an exception traceback, or the result?
[英]How do i return error in exception in python?
我想返回我在 python 中编写的代码中的错误。我不能这样做。 我该怎么做?
def proc():
try:
a=2/0
except Exception as e:
print("Except")
raise f"{e}"
else:
return "Success"
result=proc()
print("result : ",result)
我尝试使用直接加注但没有用? 我能怎么做?
如果您只想返回名称为 class 的错误消息,您可以这样做:
def proc():
try:
a=2/0
except Exception as e:
print("Except")
return repr(e) # Repr is a great solution
else:
return "Success"
result=proc()
print("result : ",result)
结果:
Except
result : ZeroDivisionError(division by zero)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.