8.1 什么是异常
Python用异常对象来表示异常情况。
事实上,每个异常都是一些类的实例,这些实例可以被引发,并且可以用很多种方法进行捕捉,使得程序可以抓住错误并且对其进行处理,而不是让整个程序失败。
BaseException 所有异常基类
+-- SystemExit python解释器请求退出
+-- KeyboardInterrupt 用户中断执行(通常是输入ctrl+C)
+-- GeneratorExit 生成器(generator)发生异常来通知退出
+-- Exception 常规错误的基类
+-- StopIteration 迭代器没有更多的值
+-- StandardError 所有的内建标准异常的基类
| +-- BufferError
| +-- ArithmeticError 所有数值计算错误的基类
| | +-- FloatingPointError 浮点计算错误
| | +-- OverflowError 数值运算超出最大限制
| | +-- ZeroDivisionError 除(或取模)零(所有数据类型)
| +-- AssertionError 断言语句失败
| +-- AttributeError 对象没有这个属性
| +-- EnvironmentError 操作系统错误的基类
| | +-- IOError 输入输出失败
| | +-- OSError 操作系统错误
| | +-- WindowsError (Windows) windows系统调用失败
| | +-- VMSError (VMS)
| +-- EOFError 没有内建输入,到达EOF标记
| +-- ImportError 导入模块/对象 失败
| +-- LookupError 无效数据查询的基类
| | +-- IndexError 序列中没有此索引(index)
| | +-- KeyError 映射中没有这个键
| +-- MemoryError 内存溢出错误(对于Python解释器不是致命的)
| +-- NameError 未声明/初始化对象(没有属性)
| | +-- UnboundLocalError 访问未初始化的本地变量
| +-- ReferenceError 弱引用(Weak reference)试图访问已经垃圾回收了的对象
| +-- RuntimeError 一般的运行时错误
| | +-- NotImplementedError 尚未实现的方法
| +-- SyntaxError Pythony语法错误
| | +-- IndentationError 缩进错误
| | +-- TabError Tab和空格混用
| +-- SystemError 一般的解释器系统错误
| +-- TypeError 对类型无效的操作
| +-- ValueError 传入无效的参数
| +-- UnicodeError Unicode相关错误
| +-- UnicodeDecodeError Unicode解码时错误
| +-- UnicodeEncodeError Unicode编码时错误
| +-- UnicodeTranslateError Unicode转换时错误
+-- Warning 警告的基类
+-- DeprecationWarning 关于被启用的特征的警告
+-- PendingDeprecationWarning 关于构造将来语义会有改变的警告
+-- RuntimeWarning 可疑的运行时行为的警告
+-- SyntaxWarning 可疑的语言的警告
+-- UserWarning 用户代码生成警告
+-- FutureWarning
+-- ImportWarning 导入模块/对象警告
+-- UnicodeWarning Unicode警告
+-- BytesWarning Bytes警告
+-- Overflow Warning 旧的关于自动提升为长整型(long)的警告
8.2.2 自定义异常类
创建自己的异常类——就像其他类一样,只是要确保从Exception类继承(不管是间接的或者是直接的,也就是说继承其他的内建异常类也是可以的)。
class SomeCustonException(Exception): pass
8.3 捕捉异常
为了捕捉异常并且做出一些错误处理里,可以这样重写程序:
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
except ZeroDivisionError:
print "The second number can't be zero!"
屏蔽机制。
8.4 不止一个except子句
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
except ZeroDivisionError:
print "The second number can't be zero!"
except TypeError:
print "That wasn't a number,was it?"
8.5 用一个块捕捉两个异常
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
except (ZeroDivisionError,TypeEror,NameError):
print "your number were bogus..."
8.6 捕捉对象
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
except (ZeroDivisionError,TypeEror),e:
print e
8.7 真正的捕捉
如果真的想用一段代码捕捉所有的异常,那么可以在except子句中忽略所有的异常类:
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x/y
except:
print "Something wrong happend..."
8.8 else
try:
print 'A simple task'
except:
print 'What? Something went wrong?'
else:
print 'Ah... It went as planned.'
只有在没有异常引发的情况下,else才会执行。
8.9 finally
x = None
try:
x = 1/0
finally:
print 'Cleaning up...'
del x
finally子句肯定会被执行,不管try子句中是否发生异常。