首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python学习笔记9——第八章 错误

2012-10-10 
python学习笔记9——第八章 异常第八章? 异常?1. ?? (1)捕捉异常,try & except try:x input(x: )y

python学习笔记9——第八章 异常

第八章? 异常

?

1.

?? (1)捕捉异常,try & except

>>> try:x = input("x: ")y = input("y: ")print x/yexcept ZeroDivisionError:print "The second number can't be zero!"x: 1y: 0The second number can't be zero!

?

?? (2)捕捉异常并重新引发

>>> class MuffledCalculator:muffled = False;  #屏蔽开关的标志def calc(self, expr):try:return eval(expr)except ZeroDivisionError:if self.muffled:  #若屏蔽开着,则捕捉异常并进行处理print "Division by zero is illegal"else:  #若屏蔽关着,则捕捉异常并重新引发raise  #except捕捉到了异常,raise重新引发异常>>> calculator = MuffledCalculator()>>> calculator.calc('10/2')5>>> calculator.calc('10/0') #屏蔽机制未打开,因为muffled = FalseTraceback (most recent call last):  File "<pyshell#29>", line 1, in <module>    calculator.calc('10/0') #屏蔽机制未打开,因为muffled = False  File "<pyshell#26>", line 5, in calc    return eval(expr)  File "<string>", line 1, in <module>ZeroDivisionError: integer division or modulo by zero>>> calculator.muffled = True  #打开屏蔽机制>>> calculator.calc('10/0')Division by zero is illegal

?

?? (3)多个except,捕捉多个不同类型的异常

>>> try:x = input("x: ")y = input("y: ")print x/yexcept ZeroDivisionError:  print "Y can't be zero!"except TypeError:print "That wasn't a numbber, was it?"x: 1y: 'test'That wasn't a numbber, was it?

?

?? (4)一个except捕捉多个类型异常,效果同(3)

>>> try:x = input("x: ")y = input("y: ")print x/yexcept(ZeroDivisionError, TypeError, NameError):  #NameError找不到名字(变量)时引发print 'Your numbers were bogus(wrong)...'

?

?? (5)捕捉异常对象,想让程序继续运行,又想记录错误时应用

>>> try:x = input("x: ")y = input("y: ")print x/yexcept(ZeroDivisionError, TypeError), e:  #多个异常时也只需提供一个参数e,是一个元组print "Invalid input:", ex: 1y: 0Invalid input: integer division or modulo by zerox: 1y: 'test'Invalid input: unsupported operand type(s) for /: 'int' and 'str'

?

?? (6)捕捉所有异常,except后不加东西

>>> try:x = input("x: ")y = input("y: ")print x/yexcept:print "Something wrong happend..."x: Something wrong happend...>>> 

?

?? (7) try & except & else

>>> while True:try:x = input("x: ")y = input("y: ")print 'x/y is', x/yexcept:print "Invalid input. Please try again."else:  #只有在异常没发生,也就是输入正确的时候,循环才退出breakx: 1y: 0x/y is Invalid input. Please try again.x: 2y: 'test'x/y is Invalid input. Please try again.x: Invalid input. Please try again.x: 1y: 2
?

?? (8) try & finally, try & except & finally, try & except & else & finally

????????? --不管是否发生异常,finally语句块一定会执行

>>> try:1/0except NameError:print "Unknown variable"else:print "That went well!"finally:print "Cleaning up."

?

?? 实例

>>> def describePerson(person):print "Description of", person['name']print 'Age:', person['age']try:print 'Occupation: ' + person['occupation']  #不能用逗号连接,若用逗号,则在异常发生前就打印了pccupationexcept KeyError:  #键不存在pass>>> person = {'name': 'Sun', 'age': 32}>>> describePerson(person)  #捕获异常并处理Description of SunAge: 32#如果最后一句为print 'Occupation:', person['occupation']#则出现>>> describePerson(person)Description of SunAge: 32Occupation:#所以要用加号连接,不用逗号,防止在异常发生前就被打印。
?

?

热点排行