温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python有哪些内置异常

发布时间:2021-10-29 14:36:46 来源:亿速云 阅读:144 作者:iii 栏目:编程语言

这篇文章主要讲解了“Python有哪些内置异常”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python有哪些内置异常”吧!

1. SyntaxError

SyntaxError主要是Python语法发生了错误,比如少个冒号、多个引号之类的,编程时稍微疏忽大意一下就会出错,应该是最常见的一种异常错误了。

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

2. TypeError

TypeError是类型错误,也就是说将某个操作或功能应用于不合适类型的对象时引发,比如整型与字符型进行加减法、在两个列表之间进行相减操作等等。

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是关键字错误,这个异常主要发生在字典中,比如当用户试图访问一个字典中不存在的键时会被引发。

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为值错误,当用户传入一个调用者不期望的值时会引发,即使这个值的类型是正确的,比如想获取一个列表中某个不存在值的索引。

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是属性错误,当用户试图访问一个对象不存在的属性时会引发,比如列表有index方法,而字典却没有,所以对一个字典对象调用该方法就会引发该异常。

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是指变量名称发生错误,比如用户试图调用一个还未被赋值或初始化的变量时会被触发。

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为打开文件错误,当用户试图以读取方式打开一个不存在的文件时引发。

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为迭代器错误,当访问至迭代器最后一个值时仍然继续访问,就会引发这种异常,提醒用户迭代器中已经没有值可供访问了。

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为断言错误,当用户利用断言语句检测异常时,如果断言语句检测的表达式为假,则会引发这种异常。

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有哪些内置异常这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI