2018-06-01 python打印错误堆栈

import logging
import traceback
def testPrintStackInfo(self):  
        try:  
            1 / 0   # 触发异常  
        except BaseException as e:  
            # 方式 1  
            msg = traceback.format_exc() 
            print (msg)  
            # 方式 2  
            logging.exception(e)    
        finally:  
            pass  

如果没有错误,那么msg为None.

你可能感兴趣的:(2018-06-01 python打印错误堆栈)