在程序開發(fā)中如果遇到一些 不可預知
的錯誤 或 你懶得做一些判斷 時,可以選擇用異常處理來做。
import requests while True: url = input("請輸入要下載網(wǎng)頁地址:") res = requests.get(url=url) with open('content.txt', mode='wb') as f: f.write(res.content)
上述下載視頻的代碼在正常情況下可以運行,但如果遇到網(wǎng)絡出問題,那么此時程序就會報錯無法正常執(zhí)行
try: res = requests.get(url=url) except Exception as e: 代碼塊,上述代碼出異常待執(zhí)行。 print("結(jié)束")
import requests while True: url = input("請輸入要下載網(wǎng)頁地址:") try: res = requests.get(url=url) except Exception as e: print("請求失敗,原因:{}".format(str(e))) continue with open('content.txt', mode='wb') as f: f.write(res.content)
num1 = input("請輸入數(shù)字:") num2 = input("請輸入數(shù)字:") try: num1 = int(num1) num2 = int(num2) result = num1 + num2 print(result) except Exception as e: print("輸入錯誤")
以后常見的應用場景:
異常處理的基本格式:
try: # 邏輯代碼 except Exception as e: # try中的代碼如果有異常,則此代碼塊中的代碼會執(zhí)行。
try: # 邏輯代碼 except Exception as e: # try中的代碼如果有異常,則此代碼塊中的代碼會執(zhí)行。 finally: # try中的代碼無論是否報錯,finally中的代碼都會執(zhí)行,一般用于釋放資源。 print("end") """ try: file_object = open("xxx.log") # .... except Exception as e: # 異常處理 finally: file_object.close() # try中沒異常,最后執(zhí)行finally關閉文件;try有異常,執(zhí)行except中的邏輯,最后再執(zhí)行finally關閉文件。 """
import requests while True: url = input("請輸入要下載網(wǎng)頁地址:") try: res = requests.get(url=url) except Exception as e: print("請求失敗,原因:{}".format(str(e))) continue with open('content.txt', mode='wb') as f: f.write(res.content)
之前只是簡單的捕獲了異常,出現(xiàn)異常則統(tǒng)一提示信息即可。如果想要對異常進行更加細致的異常處理,則可以這樣來做:
import requests from requests import exceptions while True: url = input("請輸入要下載網(wǎng)頁地址:") try: res = requests.get(url=url) print(res) except exceptions.MissingSchema as e: print("URL架構(gòu)不存在") except exceptions.InvalidSchema as e: print("URL架構(gòu)錯誤") except exceptions.InvalidURL as e: print("URL地址格式錯誤") except exceptions.ConnectionError as e: print("網(wǎng)絡連接錯誤") except Exception as e: print("代碼出現(xiàn)錯誤", e) # 提示:如果想要寫的簡單一點,其實只寫一個Exception捕獲錯誤就可以了
如果想要對錯誤進行細分的處理,例如:發(fā)生Key錯誤和發(fā)生Value錯誤分開處理。
try: # 邏輯代碼 pass except KeyError as e: # 小兵,只捕獲try代碼中發(fā)現(xiàn)了鍵不存在的異常,例如:去字典 info_dict["n1"] 中獲取數(shù)據(jù)時,鍵不存在。 print("KeyError") except ValueError as e: # 小兵,只捕獲try代碼中發(fā)現(xiàn)了值相關錯誤,例如:把字符串轉(zhuǎn)整型 int("無誒器") print("ValueError") except Exception as e: # 王者,處理上面except捕獲不了的錯誤(可以捕獲所有的錯誤)。 print("Exception")
Python中內(nèi)置了很多細分的錯誤,供你選擇。
常見異常:
"""
AttributeError 試圖訪問一個對象沒有的樹形,比如foo.x,但是foo沒有屬性x
IOError 輸入/輸出異常;基本上是無法打開文件
ImportError 無法引入模塊或包;基本上是路徑問題或名稱錯誤
IndentationError 語法錯誤(的子類) ;代碼沒有正確對齊
IndexError 下標索引超出序列邊界,比如當x只有三個元素,卻試圖訪問n x[5]
KeyError 試圖訪問字典里不存在的鍵 inf['xx']
KeyboardInterrupt Ctrl+C被按下
NameError 使用一個還未被賦予對象的變量
SyntaxError Python代碼非法,代碼不能編譯(個人認為這是語法錯誤,寫錯了)
TypeError 傳入對象類型與要求的不符合
UnboundLocalError 試圖訪問一個還未被設置的局部變量,基本上是由于另有一個同名的全局變量,
導致你以為正在訪問它
ValueError 傳入一個調(diào)用者不期望的值,即使值的類型是正確的
"""
更多異常:
"""
ArithmeticError
AssertionError
AttributeError
BaseException
BufferError
BytesWarning
DeprecationWarning
EnvironmentError
EOFError
Exception
FloatingPointError
FutureWarning
GeneratorExit
ImportError
ImportWarning
IndentationError
IndexError
IOError
KeyboardInterrupt
KeyError
LookupError
MemoryError
NameError
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
ReferenceError
RuntimeError
RuntimeWarning
StandardError
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
ZeroDivisionError
"""
上面都是Python內(nèi)置的異常,只有遇到特定的錯誤之后才會拋出相應的異常。
其實,在開發(fā)中也可以自定義異常。
class MyException(Exception): pass
try: pass except MyException as e: print("MyException異常被觸發(fā)了", e) except Exception as e: print("Exception", e)
上述代碼在except中定義了捕獲MyException異常,但他永遠不會被觸發(fā)。因為默認的那些異常都有特定的觸發(fā)條件,例如:索引不存在、鍵不存在會觸發(fā)IndexError和KeyError異常。
對于我們自定義的異常,如果想要觸發(fā),則需要使用:raise MyException()
類實現(xiàn)。
class MyException(Exception): pass try: # 。。。 raise MyException() # 。。。 except MyException as e: print("MyException異常被觸發(fā)了", e) except Exception as e: print("Exception", e)
class MyException(Exception): def __init__(self, msg, *args, **kwargs): super().__init__(*args, **kwargs) self.msg = msg try: raise MyException("xxx失敗了") except MyException as e: print("MyException異常被觸發(fā)了", e.msg) except Exception as e: print("Exception", e)
class MyException(Exception): title = "請求錯誤" try: raise MyException() except MyException as e: print("MyException異常被觸發(fā)了", e.title) except Exception as e: print("Exception", e)
案例一:你我合作協(xié)同開發(fā),你調(diào)用我寫的方法。
我定義了一個函數(shù)
class EmailValidError(Exception): title = "郵箱格式錯誤" class ContentRequiredError(Exception): title = "文本不能為空錯誤" def send_email(email,content): if not re.match("\w+@live.com",email): raise EmailValidError() if len(content) == 0 : raise ContentRequiredError() # 發(fā)送郵件代碼... # ...
你調(diào)用我寫的函數(shù)
def execute(): # 其他代碼 # ... try: send_email(...) except EmailValidError as e: pass except ContentRequiredError as e: pass except Exception as e: print("發(fā)送失敗") execute() # 提示:如果想要寫的簡單一點,其實只寫一個Exception捕獲錯誤就可以了。
案例二:在框架內(nèi)部已經(jīng)定義好,遇到什么樣的錯誤都會觸發(fā)不同的異常。
import requests from requests import exceptions while True: url = input("請輸入要下載網(wǎng)頁地址:") try: res = requests.get(url=url) print(res) except exceptions.MissingSchema as e: print("URL架構(gòu)不存在") except exceptions.InvalidSchema as e: print("URL架構(gòu)錯誤") except exceptions.InvalidURL as e: print("URL地址格式錯誤") except exceptions.ConnectionError as e: print("網(wǎng)絡連接錯誤") except Exception as e: print("代碼出現(xiàn)錯誤", e) # 提示:如果想要寫的簡單一點,其實只寫一個Exception捕獲錯誤就可以了。
案例三:按照規(guī)定去觸發(fā)指定的異常,每種異常都具備被特殊的含義。
try: # 邏輯代碼 except Exception as e: # try中的代碼如果有異常,則此代碼塊中的代碼會執(zhí)行。 finally: # try中的代碼無論是否報錯,finally中的代碼都會執(zhí)行,一般用于釋放資源。 print("end")
當在函數(shù)或方法中定義異常處理的代碼時,要特別注意finally和return。
def func(): try: return 123 except Exception as e: pass finally: print(666) func()
在try或except中即使定義了return,也會執(zhí)行最后的finally塊中的代碼。
反射,提供了一種更加靈活的方式讓你可以實現(xiàn)去 對象 中操作成員(以字符串的形式去 對象
中進行成員的操作)。
class Person(object): def __init__(self,name,wx): self.name = name self.wx = wx def show(self): message = "姓名{},微信:{}".format(self.name,self.wx) user_object = Person("華青水上","hqss666") # 對象.成員 的格式去獲取數(shù)據(jù) user_object.name user_object.wx user_object.show() # 對象.成員 的格式無設置數(shù)據(jù) user_object.name = "華青水上"
user = Person("華青水上","hqss666") # getattr 獲取成員 getattr(user,"name") # user.name getattr(user,"wx") # user.wx method = getattr(user,"show") # user.show method() # 或 getattr(user,"show")() # setattr 設置成員 setattr(user, "name", "華青水上") # user.name = "華青水上"
Python中提供了4個內(nèi)置函數(shù)來支持反射:
getattr,去對象中獲取成員
v1 = getattr(對象,"成員名稱") v2 = getattr(對象,"成員名稱", 不存在時的默認值)
setattr,去對象中設置成員
setattr(對象,"成員名稱",值)
hasattr,對象中是否包含成員
v1 = hasattr(對象,"成員名稱") # True/False
delattr,刪除對象中的成員
delattr(對象,"成員名稱")
以后如果再遇到 對象.成員 這種編寫方式時,均可以基于反射來實現(xiàn)。
class Account(object): def login(self): pass def register(self): pass def index(self): pass def run(self): name = input("請輸入要執(zhí)行的方法名稱:") # index register login xx run .. account_object = Account() method = getattr(account_object, name,None) # index = getattr(account_object,"index") if not method: print("輸入錯誤") return method()
在Python中有這么句話:一切皆對象
。 每個對象的內(nèi)部都有自己維護的成員。
對象是對象
class Person(object): def __init__(self,name,wx): self.name = name self.wx = wx def show(self): message = "姓名{},微信:{}".format(self.name,self.wx) user_object = Person("華青水上","hqss666") user_object.name
類是對象
class Person(object): title = "武沛齊" Person.title # Person類也是一個對象(平時不這么稱呼)
模塊是對象
import re re.match # re模塊也是一個對象(平時不這么稱呼)。
由于反射支持以字符串的形式去對象中操作成員【等價于 對象.成員 】,所以,基于反射也可以對類、模塊中的成員進行操作。
簡單粗暴:只要看到 xx.oo 都可以用反射實現(xiàn)。
class Person(object): title = "華青水上" v1 = Person.title print(v1) v2 = getattr(Person,"title") print(v2)
import re v1 = re.match("\w+","dfjksdufjksd") print(v1) func = getattr(re,"match") v2 = func("\w+","dfjksdufjksd") print(v2)
# 導入模塊 from importlib import import_module m = import_module("random") v1 = m.randint(1,100)
在Python中如果想要導入一個模塊,可以通過import語法導入;企業(yè)也可以通過字符串的形式導入。
示例一:
# 導入模塊 import random v1 = random.randint(1,100)
示例二:
# 導入模塊exceptions from requests import exceptions as m
# 導入模塊exceptions from importlib import import_module m = import_module("requests.exceptions")
示例三:
# 導入模塊exceptions,獲取exceptions中的InvalidURL類。 from requests.exceptions import InvalidURL
# 錯誤方式 from importlib import import_module m = import_module("requests.exceptions.InvalidURL") # 報錯,import_module只能導入到模塊級別
# 導入模塊 from importlib import import_module m = import_module("requests.exceptions") # 去模塊中獲取類 cls = m.InvalidURL
在很多項目的源碼中都會有 import_module
和 getattr
配合實現(xiàn)根據(jù)字符串的形式導入模塊并獲取成員,例如:
from importlib import import_module path = "openpyxl.utils.exceptions.InvalidFileException" module_path,class_name = path.rsplit(".",maxsplit=1) # "openpyxl.utils.exceptions" "InvalidFileException" module_object = import_module(module_path) cls = getattr(module_object,class_name) print(cls)
我們在開發(fā)中也可以基于這個來進行開發(fā),提高代碼的可擴展性。
至此Python進階中面向?qū)ο笾惓L幚砼c反射總結(jié)完畢,如有不當之處,歡迎指正!
到此這篇關于Python異常處理與反射相關問題總結(jié)的文章就介紹到這了,更多相關Python異常處理與反射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
上一篇:詳解python代碼模塊化