Python 错误类型
  C4azyw3o2e8G 2023年11月02日 94 0


Python 错误类型_语法错误

Python 程序中最常见的错误原因是某个语句不符合规定的用法。这种错误称为语法错误。Python 解释器会立即报告它,通常会附上原因。

Example: Error


>>> print "hello"SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?


Copy

在 Python 3.x 中,print 是一个内置函数,需要括号。上面的语句违反了这种用法,因此会显示语法错误。

但是很多时候,程序在运行后会导致错误,即使它没有任何语法错误。这种错误是运行时错误,称为异常。Python 库中定义了许多内置的异常。让我们看看一些常见的错误类型。

下表列出了 Python 中重要的内置异常。

例外

描述

断言错误

assert 语句失败时引发。

属性错误

对属性赋值或引用引发的。

欧费罗

当 input()函数达到文件结束条件时引发。

浮动指针错误

浮点运算失败时引发。

GeneratorExit

调用生成器的 close()方法时引发。

导入错误

找不到导入的模块时引发。

索引错误

当序列的索引超出范围时引发。

键错误

在字典中找不到键时引发。

键盘中断

当用户点击中断键(Ctrl+c 或 delete)时引发。

存储器错误

当操作耗尽内存时引发。

名称错误

当在局部或全局范围内找不到变量时引发。

notimplemontederror

由抽象方法引发。

操作系统错误

当系统操作导致系统相关错误时引发。

OverflowError

当算术运算的结果太大而无法表示时引发。

报错

当弱引用代理用于访问垃圾回收引用时引发。

运行时错误

当错误不属于任何其他类别时引发。

停止迭代

由 next()函数引发,表示迭代器不再返回任何项。

句法误差

遇到语法错误时由解析器引发。

内建 Error

当缩进不正确时引发。

TabError

当缩进由不一致的制表符和空格组成时引发。

系统误差

解释器检测到内部错误时引发。

系统退出

由 sys.exit()函数引发。

类型错误

当函数或操作应用于不正确类型的对象时引发。

unboundlocalherror

当引用函数或方法中的局部变量,但没有值绑定到该变量时引发。

UnicodeError 错误

发生与 Unicode 相关的编码或解码错误时引发。

unicodeencodererror

编码过程中出现与 Unicode 相关的错误时引发。

unicodedecodererror

解码过程中出现与 Unicode 相关的错误时引发。

unicode 翻译错误

当转换过程中出现与 Unicode 相关的错误时引发。

值错误

当函数获得类型正确但值不正确的参数时引发。

零分割错误

当除法或模块运算的第二个操作数为零时引发。

索引错误

试图访问无效索引处的项目时会抛出IndexError

Example: IndexError


>>> L1=[1,2,3]>>> L1[3]Traceback (most recent call last):File "<pyshell#18>", line 1, in <module>L1[3]IndexError: list index out of range


Copy

ModuleNotFoundError

找不到模块时抛出ModuleNotFoundError

Example: ModuleNotFoundError


>>> import notamodule
Traceback (most recent call last):File "<pyshell#10>", line 1, in <module>import notamodule
ModuleNotFoundError: No module named 'notamodule'


Copy

键错误

找不到钥匙时抛出KeyError

Example: KeyError


>>> D1={'1':"aa", '2':"bb", '3':"cc"}>>> D1['4']Traceback (most recent call last):File "<pyshell#15>", line 1, in <module>D1['4']KeyError: '4'


Copy

导入错误

找不到指定函数时抛出ImportError

Example: ImportError


>>> from math import cube
Traceback (most recent call last):File "<pyshell#16>", line 1, in <module>from math import cube
ImportError: cannot import name 'cube'


Copy

停止迭代

next()函数超出迭代器项时,抛出StopIteration

Example: StopIteration


>>> it=iter([1,2,3])>>> next(it)1>>> next(it)2>>> next(it)3>>> next(it)Traceback (most recent call last):File "<pyshell#23>", line 1, in <module>next(it)StopIteration


Copy

类型错误

当对不适当类型的对象应用操作或功能时,会抛出TypeError

Example: TypeError


>>> '2'+2Traceback (most recent call last):File "<pyshell#23>", line 1, in <module>'2'+2TypeError: must be str, not int


Copy

值错误

当函数的参数类型不合适时,会抛出ValueError

Example: ValueError


>>> int('xyz')Traceback (most recent call last):File "<pyshell#14>", line 1, in <module>int('xyz')ValueError: invalid literal for int() with base 10: 'xyz'


Copy

名称错误

找不到对象时抛出NameError

Example: NameError


>>> age
Traceback (most recent call last):File "<pyshell#6>", line 1, in <module>age
NameError: name 'age' is not defined


Copy

零分割错误

当除法中的第二个运算符为零时,抛出ZeroDivisionError

Example: ZeroDivisionError


>>> x=100/0Traceback (most recent call last):File "<pyshell#8>", line 1, in <module>x=100/0ZeroDivisionError: division by zero


Copy

键盘中断

在程序执行过程中,当用户点击中断键(通常是 Control-C)时,会抛出KeyboardInterrupt

Example: KeyboardInterrupt


>>> name=input('enter your name')enter your name^c
Traceback (most recent call last):File "<pyshell#9>", line 1, in <module>name=input('enter your name')KeyboardInterrupt


 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
C4azyw3o2e8G