Python有哪些內置異常

這篇文章主要講解了“Python有哪些內置異?!?,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Python有哪些內置異?!卑?!

新昌網站制作公司哪家好,找成都創(chuàng)新互聯公司!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、自適應網站建設等網站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯公司公司2013年成立到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選成都創(chuàng)新互聯公司。

1. SyntaxError

SyntaxError主要是Python語法發(fā)生了錯誤,比如少個冒號、多個引號之類的,編程時稍微疏忽大意一下就會出錯,應該是最常見的一種異常錯誤了。

In [1]: While True print('1')   File "<ipython-input-1-8ebf67bb4c2b>", line 1     While True print('1')           ^ SyntaxError: invalid syntax

2. TypeError

TypeError是類型錯誤,也就是說將某個操作或功能應用于不合適類型的對象時引發(fā),比如整型與字符型進行加減法、在兩個列表之間進行相減操作等等。

In [8]: a = [1,2];b = [2,3] In [9]: a-b --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-9-5ae0619f8fe1> in <module> ----> 1 a-b  TypeError: unsupported operand type(s) for -: 'list' and 'list'

3. IndexError

IndexError是指索引出現了錯誤,比如最常見下標索引超出了序列邊界,比如當某個序列m只有三個元素,卻試圖訪問m[4]。

In [16]: m = [1,2,3] In [17]: m[4] --------------------------------------------------------------------------- IndexError                                Traceback (most recent call last) <ipython-input-17-94e0dfab3ff6> in <module> ----> 1 m[4]  IndexError: list index out of range

4. KeyError

KeyError是關鍵字錯誤,這個異常主要發(fā)生在字典中,比如當用戶試圖訪問一個字典中不存在的鍵時會被引發(fā)。

In [18]: dict_ = {'1':'yi','2':'er'} In [19]: dict_['3'] --------------------------------------------------------------------------- KeyError                                  Traceback (most recent call last) <ipython-input-19-c2e43847635f> in <module> ----> 1 dict_['3']  KeyError: '3'

5. ValueError

ValueError為值錯誤,當用戶傳入一個調用者不期望的值時會引發(fā),即使這個值的類型是正確的,比如想獲取一個列表中某個不存在值的索引。

In [22]: n = [1,2,3] In [23]: n.index(4) --------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) <ipython-input-23-9a1887cf29d7> in <module> ----> 1 n.index(4)  ValueError: 4 is not in list

6. AttributeError

AttributeError是屬性錯誤,當用戶試圖訪問一個對象不存在的屬性時會引發(fā),比如列表有index方法,而字典卻沒有,所以對一個字典對象調用該方法就會引發(fā)該異常。

In [25]: dict_ = {'1':'yi','2':'er'} In [26]: dict_.index('1') --------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) <ipython-input-26-516844ad2563> in <module> ----> 1 dict_.index('1')  AttributeError: 'dict' object has no attribute 'index'

7. NameError

NameError是指變量名稱發(fā)生錯誤,比如用戶試圖調用一個還未被賦值或初始化的變量時會被觸發(fā)。

In [27]: print(list_) --------------------------------------------------------------------------- NameError                                 Traceback (most recent call last) <ipython-input-27-87ebf02ffcab> in <module> ----> 1 print(list_)  NameError: name 'list_' is not defined

8. FileNotFoundError

FileNotFoundError為打開文件錯誤,當用戶試圖以讀取方式打開一個不存在的文件時引發(fā)。

In [29]: fb = open('./list','r') --------------------------------------------------------------------------- FileNotFoundError                         Traceback (most recent call last) <ipython-input-29-1b65fe5400ea> in <module> ----> 1 fb = open('./list','r')  FileNotFoundError: [Errno 2] No such file or directory: './list'

9. StopIteration

StopIteration為迭代器錯誤,當訪問至迭代器最后一個值時仍然繼續(xù)訪問,就會引發(fā)這種異常,提醒用戶迭代器中已經沒有值可供訪問了。

In [30]: list1 = [1,2] In [31]: list2 = iter(list1) In [33]: next(list2) Out[33]: 1  In [34]: next(list2) Out[34]: 2  In [35]: next(list2) --------------------------------------------------------------------------- StopIteration                             Traceback (most recent call last) <ipython-input-35-5a5a8526e73b> in <module> ----> 1 next(list2)

10. AssertionError

AssertionError為斷言錯誤,當用戶利用斷言語句檢測異常時,如果斷言語句檢測的表達式為假,則會引發(fā)這種異常。

In [45]: list3 = [1,2]  In [46]: assert len(list3)>2 --------------------------------------------------------------------------- AssertionError                            Traceback (most recent call last) <ipython-input-46-ffd051e2ba94> in <module> ----> 1 assert len(list3)>2  AssertionError:

感謝各位的閱讀,以上就是“Python有哪些內置異常”的內容了,經過本文的學習后,相信大家對Python有哪些內置異常這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯,小編將為大家推送更多相關知識點的文章,歡迎關注!

文章名稱:Python有哪些內置異常
標題網址:http://www.muchs.cn/article14/ihioge.html

成都網站建設公司_創(chuàng)新互聯,為您提供用戶體驗App設計、網站制作、網站設計公司、ChatGPT域名注冊

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

商城網站建設