>>> f = open('xpleaf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'xpleaf'
其中:
[Errno 2] No such file or directory: 'xpleaf'
便是错误原因,可以使用try-except语句来处理上面的异常:
>>> try:
... f = open('xpleaf', 'r')
... except IOError, e:
... print 'could not open file:', e
...
could not open file: [Errno 2] No such file or directory: 'xpleaf'
try:
name = raw_input('Your name:')
except Exception:
print 'quit'
执行如下:
/usr/bin/python2.7 /home/xpleaf/PycharmProjects/Python_book/10/test.py
Your name:Traceback (most recent call last):
File "/home/xpleaf/PycharmProjects/Python_book/10/test.py", line 3, in <module>
name = raw_input('Your name:')
KeyboardInterrupt
使用BaseException:捕获所有异常(错误与非错误条件引起的)
代码如下:
try:
name = raw_input('Your name:')
except BaseException:
print 'quit'
执行如下:
/usr/bin/python2.7 /home/xpleaf/PycharmProjects/Python_book/10/test.py
Your name:quit
>>> try:
... float('foo')
... except ValueError, e:
... print 'Error Happen:', e
...
Error Happen: could not convert string to float: foo
>>>
>>> type(e)
<type 'exceptions.ValueError'>
>>> str(e)
'could not convert string to float: foo'
>>> print e
could not convert string to float: foo
>>> e.__class__
<type 'exceptions.ValueError'>
>>> e.__class__.__name__
'ValueError'
>>> e
ValueError('could not convert string to float: foo',)
在这个例子中的分析是,引发了ValueError异常,然后e就是该异常的一个实例,并且在生成这个实例e的过程中,异常参数('could not convert string to float: foo',)(注意这是一个元组),就会成为e的一个属性,而使用str(e)可以输出诊断信息的字符串,那是因为调用了该类实例的__str__()方法 。 注意,如果用一个except语句来同时捕获多个异常时,使用一个错误原因即可,因为每一个异常都会生成自己的异常参数。
再强调:
result = safe_float('foo')
print result
result2 = safe_float([])
print result2执行如下:
/usr/bin/python2.7 /home/xpleaf/PycharmProjects/Python_book/10/test.py
could not convert string to float: foo
float() argument must be a string or a number
>>> with open('xpleaf.txt', 'r') as f:
... for eachLine in f:
... print eachLine
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'xpleaf.txt'
>>> raise ValueError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError
raise exclass()
>>> raise ValueError()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError
raise exclass, args
>>> raise ValueError, 'Something wrong happen about value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Something wrong happen about value
>>>
>>> raise ValueError, ('New Error', 'Something wrong happen about value')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: ('New Error', 'Something wrong happen about value')
raise exclass(args)
>>> raise ValueError('Something wrong happen about value')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Something wrong happen about value
raise exclass, instance
>>> newError = ValueError('Something wrong happen about value')
>>> raise ValueError, newError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Something wrong happen about value
>>>
>>> newError = ValueError('Something wrong happen about value')
>>> raise IOError, newError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: Something wrong happen about value注意看异常类型和异常参数
raise instance
>>> newError = ValueError('Something wrong happen about value')
>>> raise newError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Something wrong happen about value
>>>
>>> raise newError.__class__, newError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Something wrong happen about value
raise
>>> raise
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType即达不到所描述的效果,即使前面已经有异常出现,还是会触发TypeError异常
>>> assert 1 == 1
>>> assert 1 == 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert 1 == 0, 'One does not equal zero silly!'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: One does not equal zero silly!