Python包裝異常處理方法
前言
相比java,python的異常和java中不同,python主要是防止程序異常被中止。一旦被catch后它還行往下執(zhí)行。
一、異常
1.1、忽略
pass這個(gè)關(guān)鍵字相當(dāng)于一個(gè)占位符,好比TODO是一樣的,只表示此行什么也不做,不代表其它的行代碼不執(zhí)行;
try:
print(5/0)
except ZeroDivisionError:
pass
print("ddd") #這行還是可以正常執(zhí)行的1.2、捕獲
def parse_int(s):
try:
n = int(v)
except Exception as e:
print('Could not parse, Reason:', e)
parse_int('30') ##Reason: name 'v' is not defined1.3、異常鏈
try: client_obj.get_url(url) except (URLError, ValueError, SocketTimeout): client_obj.remove_url(url)
try: client_obj.get_url(url) except (URLError, ValueError): client_obj.remove_url(url) except SocketTimeout: client_obj.handle_url_timeout(url)
try: f = open(filename) except OSError: pass
1.4、自定義
class NetworkError(Exception): pass class HostnameError(NetworkError): pass class CustomError(Exception): def __init__(self, message, status): super().__init__(message, status) self.message = message self.status = status
try: msg = s.recv() except TimeoutError as e: print(e) except RuntimeError as e: print(e.args)
1.5、拋出
try:
raise RuntimeError('It failed') #拋出新異常-raise Error
except RuntimeError as e:
print(e.args)def example():
try:
int('N/A')
except ValueError:
print("Didn't work")
raise #捕獲后再拋出二、異常的顯示方式
2.1、打印信息
try: print(5/0) except ZeroDivisionError as e: print(e.args)
2.2、控制臺(tái)警告
import warnings
warnings.simplefilter('always')
def func(x, y, log_file=None, debug=False):
if log_file is not None:
warnings.warn('log_file argument deprecated', DeprecationWarning)
func(1, 2, 'a')
#第一行日志輸出warn內(nèi)容,第二行輸出代碼內(nèi)容
/Users/liudong/personCode/python/pythonTest/app/base/base_type.py:5: UserWarning: log_file argument deprecated
warnings.warn('log_file argument deprecated')2.2、存儲(chǔ)文件
import json; numbers = [2,3,4,5,6]; fileName = "numbers.json"; with open(fileName, "w") as fileObj: json.dump(numbers, fileObj); with open(fileName, "r") as fileObj: number1 = json.load(fileObj);
到此這篇關(guān)于Python包裝異常處理方法的文章就介紹到這了,更多相關(guān)Python 異常處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)base64編碼的圖片保存到本地功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)base64編碼的圖片保存到本地功能,涉及Python針對(duì)base64編碼解碼與圖形文件輸出保存相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Centos7 Python3下安裝scrapy的詳細(xì)步驟
這篇文章主要介紹了Centos7 Python3下安裝scrapy的詳細(xì)步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
用python實(shí)現(xiàn)詞云效果實(shí)例介紹
大家好,本篇文章主要講的是用python實(shí)現(xiàn)詞云效果實(shí)例介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
使用python讀取txt文件的內(nèi)容,并刪除重復(fù)的行數(shù)方法
下面小編就為大家分享一篇使用python讀取txt文件的內(nèi)容,并刪除重復(fù)的行數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
一文搞懂Python中pandas透視表pivot_table功能
透視表是一種可以對(duì)數(shù)據(jù)動(dòng)態(tài)排布并且分類(lèi)匯總的表格格式?;蛟S大多數(shù)人都在Excel使用過(guò)數(shù)據(jù)透視表,也體會(huì)到它的強(qiáng)大功能,而在pandas中它被稱(chēng)作pivot_table,今天通過(guò)本文給大家介紹Python中pandas透視表pivot_table功能,感興趣的朋友一起看看吧2021-11-11
python操作docx寫(xiě)入內(nèi)容,并控制文本的字體顏色
今天小編就為大家分享一篇python操作docx寫(xiě)入內(nèi)容,并控制文本的字體顏色,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python 讀取xml數(shù)據(jù),cv2裁剪圖片實(shí)例
這篇文章主要介紹了Python 讀取xml數(shù)據(jù),cv2裁剪圖片實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python虛擬環(huán)境virtualenv的安裝與使用詳解
virtualenv可以用來(lái)管理互不干擾的獨(dú)立python虛擬環(huán)境,在有些場(chǎng)景下非常有用,下面這篇文章主要給大家介紹了Python虛擬環(huán)境virtualenv安裝與使用的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05

