@robert1003
想讓程式在壞掉後繼續跑?
# 我就 4 要除 0 print(1 / 0)
ZeroDivisionError Traceback (most recent call last)
<ipython-input-1-3ec96714f820> in <module>
----> 1 print(1 / 0)
ZeroDivisionError: division by zero
怎麼對付他呢?
ZeroDivisionError
就是其中一種抓住所有的 Exception
try: 你的 code except: 你的 code 爛掉就會跑來這裡
ValueError
try: [].index(1) except: print('1 not in list')
ZeroDivisionError
try: print(1 / 0) except: print('cannot divide by 0')
自己 try try 看!想辦法寫個爛掉的 code
Question:可以處理排版錯誤嗎?
try:
1 / 0
1 / 0
except Exception as e:
print(e, type(e))
IndentationError: unexpected indent
想抓住特定的錯誤?
我只想把除零的 case 抓住,其他的讓他自然噴錯就好
try: 你的 code except 你想處理的Exception的名字: 你想處理的方法 except 你想處理的另一個Exception的名字: 你想處理的方法 ...
try: print(1 / 0) # 會被抓住 except ZeroDivisionError: print('cannot divide by 0')
try: print(1 / 0) # 會被抓住 [].index(0) # 不會被抓住 except ZeroDivisionError: print('cannot divide by 0')
Question:會印出什麼?
try: print(1 / 0) # ZeroDivisionError, 會被抓住 [].index(0) # ValueError, 會被抓住 [].find(0) # AttributeError, 不會被抓住 except ZeroDivisionError: print('cannot divide by 0') except ValueError: print('0 not in list')
try-except-except 會執行到第一個錯誤為止
不管有沒有發生錯誤都要執行的東西
finally
try: 你的 code except 處理其中一種 Exception: 處理的 code finally: 無論如何都會跑的 code
try: print(1 / 0) except ZeroDivisionError: print('zero division error') finally: print('finally')
跟這個差在哪?
try: print(1 / 0) except ZeroDivisionError: print('zero division error') print('finally')
你有 Exception 沒處理到的時候就會有差
try: [].index(0) # 不會被抓住 except ZeroDivisionError: print('zero division error') finally: print('finally')
誰說自己不能生錯誤呢
自己噴錯
raise 你想噴的Exception instance
亂噴錯
x = int(input()) if x == 5: raise Exception("I don't like 5, go away")
亂噴錯2
x = int(input()) if x == 5: raise ZeroDivisionError("I don't like 5, go away")
小提醒:自己寫扣的時候亂噴錯沒關係,但跟別人合作的時候不能這樣,因為別人會看不懂為什麼爛掉
小練習:試試看自己 raise 一個 Exception 然後用上面教的 try-catch 把他抓住
自己寫Exception Class
class 你的Exception名字(Exception): # 繼承 Exception 這個 Base exception class pass
「我不喜歡5」Exception
class IDontLikeFiveError(Exception): pass x = int(input()) if x == 5: raise IDontLikeFiveError("我不喜歡5")
小提醒:
小練習:自己建立一個 Exception Class 然後 raise 他
客製化自己的 Exception Class
class IDontLikeError(Exception): def __init__(self, what, reason): self.what = what self.reason = reason def __str__(self): return "I don't like {} because {}".format(self.what, self.reason) x = int(input()) raise IDontLikeError(x, "我就4不喜歡")
小練習:客製化自己剛剛的 Exception Class,讓他的 error message 都加上一個 prefix "我就4不喜歡:"