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)
时间: 2024-11-06 11:25:35