2018-10-23
閱讀量:
1108
python錯(cuò)誤解析(五)
代碼如下:
bad = 'bad'
try:
raise bad
except bad:
print 'Got Bad!'
錯(cuò)誤:
Traceback (most recent call last):
File "D:\Learn\Python\Learn.py", line 4, in <module>
raise bad
TypeError: exceptions must be old-style classes or derived from BaseException, not str
【錯(cuò)誤分析】因所用的Python版本2.7,比較高的版本,raise觸發(fā)的異常,只能是自定義類異常,而不能是字符串。所以會(huì)報(bào)錯(cuò),字符串改為自定義類,就可以了。
代碼如下:
class Bad(Exception):
pass
def raiseException():
raise Bad()
try:
raiseException()
except Bad:
print 'Got Bad!'






評(píng)論(0)


暫無數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論