Configuring Python's default exception handling

后端 未结 1 1488
余生分开走
余生分开走 2021-02-02 13:09

For an uncaught exception, Python by default prints a stack trace, the exception itself, and terminates. Is anybody aware of a way to tailor this behaviour on the program level

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 13:55

    You are looking for sys.excepthook:

    sys.excepthook(type, value, traceback)

    This function prints out a given traceback and exception to sys.stderr.

    When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.

    0 讨论(0)
提交回复
热议问题