若是錯誤沒有被捕獲,它就會一直往上拋,最後被Python解釋器捕獲,打印一個錯誤信息,而後程序退出spa
經過錯誤信息,能夠追根溯源查到最終的錯誤緣由code
def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): bar('0') main() #輸出: Traceback (most recent call last): File "err.py", line 11, in <module> main() File "err.py", line 9, in main bar('0') File "err.py", line 6, in bar return foo(s) * 2 File "err.py", line 3, in foo return 10 / int(s) ZeroDivisionError: division by zero