python是否有“全部捕获”解决方案,如果在脚本中的任何地方触发错误/异常,则错误将传递给类似于PHP的set_error_handler()函数的自定义函数?
我熟悉try / catch方法,但它是一种新生,因为try / catch仅适用于其中的直接代码。
def sample_function():
# some code that causes error, breaks script, does not pass error back to try/catch
try:
sample_function()
except Exception, e:
print str(e)
我正在寻找一个简单的尝试/除了一切,即使在函数内调用的函数中触发错误,而不必用try / except包装每个代码块。
有人知道这是否可行?
答案 0 :(得分:3)
代码中的某个地方是主要入口点。只需将try / except包裹起来。
答案 1 :(得分:1)
在我看来,你可以为sys.excepthook