Python實(shí)現(xiàn)捕獲異常發(fā)生的文件和具體行數(shù)
我就廢話不多說啦,還是直接看代碼吧!
try: print(a)except Exception as e: print(e) print(e.__traceback__.tb_frame.f_globals['__file__']) # 發(fā)生異常所在的文件 print(e.__traceback__.tb_lineno) # 發(fā)生異常所在的行數(shù)
補(bǔ)充知識:catch中打出完整錯誤日志,包括Exception類型和報(bào)錯行數(shù)
try(){}catch(Exception e){}
在catch中打出完整錯誤日志,包括Exception類型和報(bào)錯行數(shù)
方法:在catch塊中加入代碼
1)
ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); String exception = baos.toString(); System.out.println('baos:' + exception);
2)
logger.error('',e);
以上這篇Python實(shí)現(xiàn)捕獲異常發(fā)生的文件和具體行數(shù)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
