異常該怎么抓住


1.普通異常的使用:

names = ['wt','gxb']
data = {}
try:
    # names[3]
    # data['name']
    open('b.txt')
except IndexError as e:
    print('there is not exist this key',e)
except Exception as e:
    print('I can`t find this error!!!')
else:
    print('it`s ok!!')
finally:
    print('dajkdhkaj')

2.自己觸發的異常:

class MyExpection(Exception):
    def __init__(self,msg):
        self.message = msg

try:
    raise MyExpection('觸發了異常')
except MyExpection as e:
    print(e)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM