【发布时间】:2020-07-19 16:10:03
【问题描述】:
我正在尝试理解 Python 中 try 和 except 的概念,我知道这可以用来解决代码中的问题,而不会破坏应用程序的流程。
我正在开发一个具有客户端和开发人员端的应用程序。因此,我不想向用户显示确切的错误是什么,而是他们可以看到的代码,然后报告和发布它,我可以根据该代码找到错误。我不知道该怎么做。
我研究了主题Manually raising an exception in Python。
我的程序有多个通过计算连接在一起的函数,即
def function_1(_input_):
# do some jobs here get the result then,
result = function_2(result)
return result
def function_2(_input_):
# do some jobs here and get the result then,
result = function_3(result)
return result
...
我想通过错误消息和导致问题的函数来捕捉在此过程中发生的错误。 我已经实现了这样的东西:
def function_1(_input_):
try:
# do some jobs here get the result then,
result = function_2(result)
except Exception as e:
print(e)
return result
def function_2(_input_):
try:
# do some jobs here and get the result then,
result = function_3(result)
except Exception as e:
print(e)
return result
...
【问题讨论】:
标签: python exception try-except