Python sys 模块,_excepthook() 实例源码
我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用sys._excepthook()。
def exception_hook(exception_type, message, traceback):
"""
An exception hook that will display a GUI and optionally allow the user
to submit a GitHub issue.
:param exception_type:
The type of exception that was raised.
:param message:
The exception message.
:param traceback:
The traceback of the exception.
"""
# Show the exception in the terminal.
sys._excepthook(exception_type, message, traceback)
# Should this exception be ignored?
if message.__repr__() in ignore_exception_messages:
return None
# Load a GUI that shows the exception.
exception_gui = exception.ExceptionWidget(
exception_type, message, traceback)
exception_gui.exec_()
# Ignore future exceptions of this kind?
if exception_gui.ignore_in_future:
ignore_exception_messages.append(message.__repr__())
return None
def exception_hook(exception_type, message, traceback):
"""
An exception hook that will display a GUI and optionally allow the user
to submit a GitHub issue.
:param exception_type:
The type of exception that was raised.
:param message:
The exception message.
:param traceback:
The traceback of the exception.
"""
# Show the exception in the terminal.
sys._excepthook(exception_type, message, traceback)
# Should this exception be ignored?
if message.__repr__() in ignore_exception_messages:
return None
# Load a GUI that shows the exception.
exception_gui = exception.ExceptionWidget(
exception_type, message, traceback)
exception_gui.exec_()
# Ignore future exceptions of this kind?
if exception_gui.ignore_in_future:
ignore_exception_messages.append(message.__repr__())
return None
def exception_hook(exception_type, message, traceback):
"""
An exception hook that will display a GUI and optionally allow the user
to submit a GitHub issue.
:param exception_type:
The type of exception that was raised.
:param message:
The exception message.
:param traceback:
The traceback of the exception.
"""
# Show the exception in the terminal.
sys._excepthook(exception_type, message, traceback)
# Should this exception be ignored?
if message.__repr__() in ignore_exception_messages:
return None
# Load a GUI that shows the exception.
exception_gui = exception.ExceptionWidget(
exception_type, message, traceback)
exception_gui.exec_()
# Ignore future exceptions of this kind?
if exception_gui.ignore_in_future:
ignore_exception_messages.append(message.__repr__())
return None
def my_exception_hook(exception_type, value, traceback):
# Print the error and traceback
print(exception_type, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exception_type, value, traceback)
sys.exit(1)
def main():
print("init")
sys._excepthook = sys.excepthook
sys.excepthook = my_exception_hook
app = QApplication(sys.argv)
win = MainWindow()
# noinspection PyBroadException
try:
sys.exit(app.exec_())
except:
print("Exiting")
def main():
sys._excepthook = sys.excepthook
sys.excepthook = my_exception_hook
app = QtWidgets.QApplication(sys.argv)
get_splash(app)
setDarkmode(app)
mainWindow = QtWidgets.QMainWindow()
ui = MainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)