處理多個異常
處理多個異常花枫,并不是同時報(bào)出多個異常刻盐。程序運(yùn)行時,只要遇到一個異常劳翰,就會有所反應(yīng)敦锌。所以,每次捕獲到的異常佳簸,一定是一個乙墙。所以,所謂的處理多個異常,意思是說容許捕獲不同的異常听想,在不同的except子句中處理腥刹。
形式如下:
try:
print "try子句"
except xxx:
print "異常xxx"
except yyy:
print "異常yyy"
例子:
#!usr/bin/env Python
# coding=utf-8
while 1:
print "this is a division program."
c = raw_input("input 'c' continue,otherwise logot:")
if c == 'c':
a = raw_input("first number:")
b = raw_input("second number:")
try:
print float(a)/float(b)
except ZeroDivisionError:
print "the second number can't be zero"
print "************************"
except ValueError:
print "please input number"
print "************************"
else:
break
與上一篇的例子相比,多了一個異常捕獲汉买。當(dāng)輸入的分母不是數(shù)字時衔峰,可以捕獲該異常。測試如下:
$ python tryExcept1.py
this is a division program.
input 'c' continue,otherwise logot:c
first number:3
second number:a
please input number
************************
this is a division program.
input 'c' continue,otherwise logot:c
first number:3
second number:0
the second number can't be zero
************************
this is a division program.
input 'c' continue,otherwise logot:c
first number:5
second number:2
2.5
this is a division program.
input 'c' continue,otherwise logot:p
$
除了使用多個except之外蛙粘,還可以在一個except后面放多個異常參數(shù)垫卤,可以將except部分修改為如下:
except (ZeroDivisionError,ValueError):
print "please input rightly"
print "*********************"
運(yùn)行結(jié)果:
$ python tryExcept1.py
this is a division program.
input 'c' continue,otherwise logot:c
first number:2
second number:a
please input rightly
*********************
this is a division program.
input 'c' continue,otherwise logot:c
first number:3
second number:0
please input rightly
*********************
this is a division program.
input 'c' continue,otherwise logot:c
first number:4
second number:2
2.0
this is a division program.
input 'c' continue,otherwise logot:d
mobao:PythonExample mobao$
特別注意,一個except组题,多個異常參數(shù)時葫男,異常參數(shù)需要放進(jìn)()中。
如果需要呈現(xiàn)內(nèi)置的異常信息崔列,則可做如下更改:
except (ZeroDivisionError,ValueError), e:
print e
print "*********************"
在Python3中梢褐,則如下:
except (ZeroDivisionError,ValueError) as e:
print e
print "*********************"
運(yùn)行效果:
python tryExcept1.py
this is a division program.
input 'c' continue,otherwise logot:c
first number:2
second number:a
could not convert string to float: a
*********************
this is a division program.
input 'c' continue,otherwise logot:c
first number:2
second number:0
float division by zero
*********************
this is a division program.
"could not convert string to float: a"和"float division by zero"皆為內(nèi)置異常信息。
以上的寫法赵讯,處理了兩個異常盈咳,如果是不止兩個異常呢?可以這樣:execpt: 或者 except Exception, e边翼,后面什么參數(shù)也不寫就好了鱼响。
else子句
一般try...except...,在一般情況下是夠用的组底。但是總有不一般的時候出現(xiàn)丈积,所以增加了一個else子句。如:
>>> try:
... print "I am try"
... except:
... print "I am except"
... else:
... print "I am else"
...
I am try
I am else
可以看出债鸡,如果執(zhí)行了try江滨,則except()子句會被直接忽略,else子句會被執(zhí)行厌均。
>>> try:
... print 1/0
... except:
... print "I am except"
... else:
... print "I am else"
...
I am except
如果執(zhí)行了except子句唬滑,則不執(zhí)行else中的文件。
例如:
#!usr/bin/env Python
# coding=utf-8
while 1:
try:
a = raw_input("first number:")
b = raw_input("second number:")
print float(a)/float(b)
except Exception, e:
print e
print "try again"
else:
break
結(jié)果如下:
$ python tryExcept1.py
first number:2
second number:a
could not convert string to float: a
try again
first number:3
second number:0
float division by zero
try again
first number:5
second number:2
2.5
$
finally
無論try子句執(zhí)行還是except子句執(zhí)行棺弊,finally中一定執(zhí)行晶密。
>>> x = 10
>>> try:
... x = 2/1
... except Exception,e:
... print e
... finally:
... print "del x"
... del x
...
del x
>>> x = 10
>>> try:
... x = 1/0
... except Exception,e:
... print e
... finally:
... print "del x"
... del x
...
integer division or modulo by zero
del x
當(dāng)然,在應(yīng)用中模她,可以將上面的各個子句都綜合起來使用稻艰,寫成如下樣式:
try:
do something
except:
do something
else:
do something
finally
do something