Talk Is Cheap
和Java一样,python也提供了对于checked exception和unchecked exception. 对于checked exception,我们通常使用try except可以显示解决,对于unchecked 异常,其实也是提供回调或者是钩子来帮助我们处理的,我们可以在钩子里面记录崩溃栈追踪或者发送崩溃数据.
下面代码可以实现python unchecked exception回调,并输出日志信息.
Show Me The Code
复制代码 代码如下:
import os, sys
import logging
logger = logging.getLogger(name)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.excepthook(exc_type, exc_value, exc_traceback)
return
logger.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
if name == "main":
raise RuntimeError("Test unhandled Exception")
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8