網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python打印完整異常
import traceback if __name__ == "__main__": try: print(1/0) except Exception: print(traceback.format_exc())
python異常捕獲與打印
為什么要進(jìn)行異常捕獲?
在python程序中如果代碼中有異常,不進(jìn)行異常捕獲的話,代碼遇到異常就會(huì)終止運(yùn)行,影響程序的健壯性,故要進(jìn)行異常捕獲處理,使得程序遇到不可知的異常,不會(huì)終止,在exception代碼塊中進(jìn)行異常處理后繼續(xù)后面的代碼執(zhí)行。
異常捕獲是捕獲什么?
* 異常類型
* 異常追蹤信息
* ?異常內(nèi)容
異常捕獲的代碼格式?
try: ? ? {content code} except Exception as e: ? ? print("substitude exception,type={},content={}".format(repr(e),e))#y else: ? ? print("code exec correct")#沒(méi)有遇到異常,可以省略 finally: ? ? ?print("finnally echo")#無(wú)論是否遇到異常都會(huì)執(zhí)行這段內(nèi)容,先執(zhí)行exception處理,后執(zhí)行finally
利用raise自己定義異常并拋出,拋出的異常必須繼承一個(gè)已有的異常類型,如TypeError(“param must be dict”)
def func3(p): ? ? if type(p)!= dict: ? ? ? ? raise TypeError("param must be dict") ? ? else: ? ? ? ? print(p) if __name__ == '__main__': ?try: ? ? func3("123") ?except Exception as e: ? ? print("exception,type={},content={}".format(repr(e),e)) ?finally: ? ? ?print("finnally echo") ? ? -------------------- ? ? 打印結(jié)果 ? ? exception,type=TypeError('param must be dict'),content=param must be dict finnally echo
打印異常
代碼舉例:
try: ? ? str2json = json.loads(str) ? ? return jsonpath.jsonpath(str2json,expr) except Exception as e: ? ? cls.logger.info('collectjson exception = {}'.format(str(e)))
repr(e) 打印異常的類型 ,如在json轉(zhuǎn)字符串時(shí),傳入一個(gè)字符串,那么會(huì)拋異常,repr(e)打印的是 JSONDecodeError('Extra data: line 1 column 8 (char 7)',清晰的看出來(lái)這個(gè)是json轉(zhuǎn)換出了問(wèn)題
str(e) 打印問(wèn)題代碼的具體問(wèn)題行
Traceback (most recent call last):
? File "D:/python-workspace/iaminterfaceTest/jsonUtil.py", line 14, in collectjson
? ? str2json = json.loads(str)
? File "D:\python37\Lib\json\__init__.py", line 348, in loads
? ? return _default_decoder.decode(s)
? File "D:\python37\Lib\json\decoder.py", line 340, in decode
? ? raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 8 (char 7)
常見(jiàn)異常
-
IOError
文件讀寫(xiě)異常,一般是文件不存在或者文件不可寫(xiě)入 -
ImportError
無(wú)法導(dǎo)入模塊或者對(duì)象,主要是路徑有誤或名稱錯(cuò)誤 -
TypeError
類型轉(zhuǎn)換異常 -
KeyError ditc
讀取key值不對(duì),一般是不存在這個(gè)key值
原文鏈接:https://blog.csdn.net/weixin_45592677/article/details/121354794
相關(guān)推薦
- 2022-11-17 Android顯式Intent與隱式Intent的使用詳解_Android
- 2022-01-19 webpack5 配置熱更新失效問(wèn)題
- 2023-01-19 C++?new與malloc和delete及free動(dòng)態(tài)內(nèi)存管理及區(qū)別介紹_C 語(yǔ)言
- 2023-03-15 pandas中fillna()函數(shù)填充NaN和None的實(shí)現(xiàn)_python
- 2022-06-12 詳解Flutter如何讀寫(xiě)文本文件_Android
- 2022-10-22 關(guān)于redis的延遲雙刪策略總結(jié)_Redis
- 2022-12-30 Python利用tkinter和socket實(shí)現(xiàn)端口掃描_python
- 2022-04-22 Number精度超了如何解決
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支