iT邦幫忙

DAY 15
0

Python初學起步走系列 第 15

[Python初學起步走-Day15] - 例外處理

當Python程式遇到錯誤情況的時候會產生例外

例如

#exception.py
print(a)

NameError: name 'a' is not defined 就是錯誤的原因

Python 可以使用try...except把例外給處理掉,語法如下

try:
    嘗試執行的程式
except 例外名稱 as 變數名稱:
    例外發生時執行的程式
else:
    若try沒產生例外則會執行這裡
finally:
    不管有沒有發生例外都會跑到的程式

except區塊可以多個,至少一個, as 變數名稱 可以用或不用, 變數會儲存例外的狀況

else與finally可選擇用或不用

例如

#exception.py
try:
    print(a)
except NameError as n:
    print("跳至except區塊")
    print("錯誤原因:",n)
finally:
    print("不管怎樣這裡都會執行到")

Python內建的例外層級結構

BaseException 
 +--  SystemExit 
 +--  KeyboardInterrupt 
 +--  GeneratorExit 
 +--  Exception 
      +--  StopIteration 
      +--  ArithmeticError 
      |     +--  FloatingPointError 
      |     +--  OverflowError 
      |     +--  ZeroDivisionError 
      +--  AssertionError 
      +--  AttributeError 
      +--  BufferError 
      +--  EOFError 
      +--  ImportError 
      +--  LookupError 
      |     +--  IndexError 
      |     +--  KeyError 
      +--  MemoryError 
      +--  NameError 
      |     +--  UnboundLocalError 
      +--  OSError 
      |     +--  BlockingIOError 
      |     +--  ChildProcessError 
      |     +--  ConnectionError 
      |     |     +--  BrokenPipeError 
      |     |     +--  ConnectionAbortedError 
      |     |     +--  ConnectionRefusedError 
      |     |     +--  ConnectionResetError 
      |     +--  FileExistsError 
      |     +--  FileNotFoundError 
      |     +--  InterruptedError 
      |     +--  IsADirectoryError 
      |     +--  NotADirectoryError 
      |     +--  PermissionError 
      |     +--  ProcessLookupError 
      |     +--  TimeoutError 
      +--  ReferenceError 
      +--  RuntimeError 
      |     +--  NotImplementedError 
      +--  SyntaxError 
      |     +--  IndentationError 
      |          +--  TabError 
      +--  SystemError 
      +--  TypeError 
      +--  ValueError 
      |     +--  UnicodeError 
      |          +--  UnicodeDecodeError 
      |          +--  UnicodeEncodeError 
      |          +--  UnicodeTranslateError 
      +--  Warning 
           +--  DeprecationWarning 
           +--  PendingDeprecationWarning 
           +--  RuntimeWarning 
           +--  SyntaxWarning 
           +--  UserWarning 
           +--  FutureWarning 
           +--  ImportWarning 
           +--  UnicodeWarning 
           +--  BytesWarning 
           +--  ResourceWarning

例如

#exception.py
try:
    a = 3/0
except ArithmeticError as n:
    print("跳至ArithmeticError 區塊")
    print("錯誤原因:",n)
except ZeroDivisionError as n:
    print("跳至ZeroDivisionError 區塊")
    print("錯誤原因:",n)

我們真正想得到的例外是ZeroDivisionError ,但由於ArithmeticError的層級在ZeroDivisionError之上,所以遇到使用多個except時會建議把細項(最深層)的擺在最前面

例如

#exception.py
try:
    a = 3/0
except ZeroDivisionError as n:
    print("跳至ZeroDivisionError 區塊")
    print("錯誤原因:",n)
except ArithmeticError as n:
    print("跳至ArithmeticError 區塊")
    print("錯誤原因:",n)


上一篇
[Python初學起步走-Day14] - List Comprehensions
下一篇
[Python初學起步走-Day16] - 引發例外
系列文
Python初學起步走30

1 則留言

0
草原狼walfes
iT邦新手 5 級 ‧ 2019-08-12 17:33:44

前輩
try:
print(a)
except NameError as n:
print("跳至except區塊")
print("錯誤原因:",n)
finally:
print("不管怎樣這裡都會執行到")
會出現
4
不管怎樣這裡都會執行到
我發現之前練習程式中有訂義a=4
我能在執行程式前,先將暫存區給清空嗎
需要下甚麼命令??麻煩指導一下

我要留言

立即登入留言