011 python語法_錯誤處理 try except finally raise
阿新 • • 發佈:2018-11-03
''' 時間:2018/11/03 目錄: 一: 正常執行 1 正常 2 else 二: 發生錯誤 1 異常 2 異常 三: BaseException 四: 多層錯誤 - 多層捕獲 五: 記錄錯誤 六: 丟擲錯誤 '''
一: 正常執行
1 正常
# coding:utf-8
try: print("try...") r = 10 / 2 # 正常執行 print("result:", r) except ZeroDivisionError as e: print("except:", e) finally: print("finally...") print("END")
try... result: 5.0 finally... END
2 else
# coding:utf-8
try: print('try...') r = 10 / int('2') print('result:', r) except ValueError as e: print('ValueError:', e) except ZeroDivisionError as e: print('ZeroDivisionError:', e) else: print('no error!') # 沒有錯誤 - 執行else finally: print('finally...') print('END')
try... result: 5.0 no error! finally... END
二: 發生錯誤
1 異常
# coding:utf-8 try: print("try...") r = 10 / 0 #發生異常 print("result:", r) except ZeroDivisionError as e: # 除法錯誤 print("except:", e) finally: print("finally...") print("END")
try... except: division by zero finally... END
2 異常
# coding:utf-8 try: print('try...') r = 10 / int('a') print('result:', r) except ValueError as e: # 變數錯誤 print('ValueError:', e) except ZeroDivisionError as e: # 除法錯誤 print('ZeroDivisionError:', e) finally: print('finally...') print('END')
try... ValueError: invalid literal for int() with base 10: 'a' finally... END
三: BaseException
# coding:utf-8 def foo(): return 10 / 0 try: foo() except BaseException as e: # 所有錯誤 print('BaseException', e) except ZeroDivisionError as e: # 除法錯誤 print('ZeroDivisionError:', e) except ValueError as e: # 變數錯誤 print('ValueError', e) except UnicodeError as e: # 編碼錯誤 print('UnicodeError', e)
BaseException division by zero
1 : BaseException接管 - 除法錯誤
# coding:utf-8 def foo(): return 10 / int('a') try: foo() except BaseException as e: # 所有錯誤 print('BaseException', e) except ZeroDivisionError as e: # 除法錯誤 print('ZeroDivisionError:', e) except ValueError as e: # 變數錯誤 print('ValueError', e) except UnicodeError as e: # 編碼錯誤 print('UnicodeError', e)
BaseException invalid literal for int() with base 10: 'a'
1 : BaseException接管 - 變數錯誤
1 : python3.6.7 錯誤型別的繼承關係。
四: 多層錯誤 - 多層捕獲
# coding:utf-8 def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): try: print("try...") bar('0') print("try...end") except Exception as e: print('Error:', e) finally: print('finally...') main()
1 : try...except... 跨越多層呼叫。
# coding:utf-8 def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): bar('0') main()
Traceback (most recent call last): File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 12, in <module> main() File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 10, in main bar('0') File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 7, in bar return foo(s) * 2 File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 4, in foo return 10 / int(s) ZeroDivisionError: division by zero
1 : 如果錯誤沒被程式捕獲,最終會被python直譯器捕獲
五: 記錄錯誤
# coding:utf-8 import logging def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): try: print("try...") bar('0') print("try...end") except Exception as e: print("except...") logging.exception(e) finally: print("finally...") main() print('END')
try... except... finally... END ERROR:root:division by zero Traceback (most recent call last): File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 14, in main bar('0') File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 9, in bar return foo(s) * 2 File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 6, in foo return 10 / int(s) ZeroDivisionError: division by zero
1 : logging模組可以讓程式繼續執行,並記錄錯誤資訊,把錯誤記錄到日誌檔案裡。
六: 丟擲錯誤
# coding:utf-8 def foo(s): n = int(s) if n==0: raise ValueError('invalid value: %s' % s) return 10 / n def bar(): try: foo('0') except ValueError as e: print('ValueError!') raise # 捕獲錯誤 - 暫不處理 bar()
ValueError! File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 16, in <module> bar() File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 11, in bar foo('0') File "D:/ProgramTools/PyCharm 5.0.4/PycharmProject/python_get/study001/study001.py", line 6, in foo raise ValueError('invalid value: %s' % s) ValueError: invalid value: 0