Python 异常类型总结

以下表格内容参考自:https://fishc.com.cn/thread-45814-1-1.html

1.异常类型:

AssertionError断言语句(assert)失败
AttributeError尝试访问未知的对象属性
EOFError用户输入文件末尾标志EOF(Ctrl+d)
FloatingPointError浮点计算错误
GeneratorExitgenerator.close()方法被调用的时候
ImportError导入模块失败的时候
IndexError索引超出序列范围
KeyError字典中查找的关键字不存在
KeyboardInterrupt用户输入中断键(Ctrl+c)
MemoryError内存溢出(可通过删除对象释放内存)
NameError尝试访问一个不存在的变量
NotImplementedError尚未实现的方法
OSError操作系统产生的异常(例如打开一个不存在的文件)
OverflowError数值运算超出最大限制
ReferenceError弱引用(weak reference)试图访问一个已经被垃圾回收机制回收了的对象
RuntimeError运行时错误
StopIteration迭代器没有更多的值
SyntaxErrorpython语法错误
IndentationError缩进错误
TabErrorTab和空格混合使用
SystemErrorpython编译器系统错误
SystemExitpython编译器进程被关闭
TypeError不同类型间的操作无效
UnboundLocalError访问一个未初始化的本地变量(NameError的子类)
UnicodeErrorUnicode相关的错误(ValueError的子类)
UnicodeEncodeErrorUnicode编码时的错误(UnicodeError的子类)
UnicodeDecodeErrorUnicode解码时的错误(UnicodeError的子类
UnicodeTranslateErrorUnicode转换时的错误(UnicodeError的子类)
ValueError传入无效的参数
ZeroDivisionError除数为0

2.异常类的层次结构:

BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
      +-- StopIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

简单举例说明几个常见异常:

>>> #AsseError:assert断言语句,当assert后面的表达式为假时抛出异常
>>> assert 3>2
>>> assert 3<@
SyntaxError: invalid syntax
>>> assert 3<2
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    assert 3<2
AssertionError
>>> 
>>> #AttributeError:尝试访问对象不存在的属性或方法
>>> list1=[2,1,3,5,4]
>>> list1.sorted()      #sorted()时python的内置函数,列表的排序函数时sort()
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    list1.sorted()
AttributeError: 'list' object has no attribute 'sorted'
>>> list1.sort()
>>> list1
[1, 2, 3, 4, 5]
>>> 
>>> #IndexError:超出序列索引范围
>>> list1=[2,1,2]
>>> list1[4]
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    list1[4]
IndexError: list index out of range
>>> 
>>> #KeyError:在字典中查找不存在的关键字
>>> dictinary={'A':'Ammy','B':'Binary','C':'Cindy'}
>>> dictinary['D']
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    dictinary['D']
KeyError: 'D'
>>> 
>>> #NameError:尝试访问一个不存在的变量
>>> a
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    a
NameError: name 'a' is not defined
>>> 
>>> #OSError:操作系统产生的异常,例如打开一个不存在的文件
>>> file=open(r'E:\helloWorld.txt','r')
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    file=open(r'E:\helloWorld.txt','r')
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\helloWorld.txt'
>>> 
>>> #TyprError:类型错误
>>> 1+'2'
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    1+'2'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 
>>> #ZeroDivisionError:除数为0
>>> 1/0
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    1/0
ZeroDivisionError: division by zero

注:使用raise可以使代码自动引发一个异常

>>> raise TypeError       #自动引发一个异常
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    raise TypeError
TypeError

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值