try....exception....finally
生活随笔
收集整理的這篇文章主要介紹了
try....exception....finally
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class MyException(Exception):def __init__(self,msg):self.msg=msgdef __str__(self):return self.msgtry:print('start')raise MyException('this is a custom exception')##手動觸發會被Exception捕獲
except Exception as e:print(e)
finally:print('end')
##ret start this is a custom exception end import time try:print('start...')time.sleep(10) except KeyboardInterrupt as e:##按ctrl+c會執行這個語句塊print('you press ctrl+c') finally:print('end...')
myexception=MyException('231') print(hasattr(myexception,'msg'))#True print(hasattr(myexception,'msg1'))#False print(getattr(myexception,'__str__')())#231 setattr(myexception,'fun',lambda x:x) print(getattr(myexception,'fun')(1234))#1234 delattr(myexception,'fun')
##ret start this is a custom exception end import time try:print('start...')time.sleep(10) except KeyboardInterrupt as e:##按ctrl+c會執行這個語句塊print('you press ctrl+c') finally:print('end...')
myexception=MyException('231') print(hasattr(myexception,'msg'))#True print(hasattr(myexception,'msg1'))#False print(getattr(myexception,'__str__')())#231 setattr(myexception,'fun',lambda x:x) print(getattr(myexception,'fun')(1234))#1234 delattr(myexception,'fun')
?
try: l = ["a", "b"] int(l[2]) except (ValueError, IndexError) as e: pass?
轉載于:https://www.cnblogs.com/howhy/p/7828340.html
總結
以上是生活随笔為你收集整理的try....exception....finally的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高级T-SQL第1级的阶梯:使用交叉连接
- 下一篇: JAVAWEB 一一 SpringMVC