Python--19 Exception 你不可能总是对的

逻辑错误,用户输入有误

Python标准异常总结

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

 

以下是 Python 内置异常的次构:

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

出现的异常举例

 

>>> my_list = ['i love you']
>>> assert len(my_list) > 0
>>> my_list.pop()
'i love you'
>>> assert len(my_list) > 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError

 

>>> my_list.aa
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'aa'

 

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

 

 

 

>>> my_dict = {'one':1,'two':2, 'three':3}
>>> my_dict['one']
1
>>> my_dict['four']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'four'

 

>>> my_dict.get('four')

 

posted @ 2017-09-11 12:55  110528844  阅读(176)  评论(0编辑  收藏  举报