class MyException(Exception):
def __init__(self,msg):
self.msg=msg
def __str__(self):
return self.msg
try:
print(‘start‘)
raise MyException(‘this is a custom exception‘)##手动触发会被Exception捕获
except Exception as e:
print(e)
finally:
print(‘end‘)
##ret
import time try: print(‘start...‘) time.sleep(10) except KeyboardInterrupt as e:##按ctrl+c会执行这个语句块 print(‘you press ctrl+c‘) finally: print(‘end...‘)
原文:http://www.cnblogs.com/howhy/p/7828340.html