Exception 例外

例外處理

最簡單

語法:

try:
    例外
except:
    處理例外
1
2
3
4
5
6
try:
    num1 = 10
    num2 = 0
    res = num1 / num2
except:
    print("出現例外")
出現例外

取得Exception

語法:

try:
    例外
except 例外 as 自訂名稱:
    處理例外

try:
    可能有例外的程式碼
except Exception as e:
    取得e例外內容
1
2
3
4
5
6
7
try:
    num1 = 10
    num2 = 0
    res = num1 / num2
except Exception as e:
    print(f"exception: {e} \n"
          "type:", type(e))
exception: division by zero 
type: <class 'ZeroDivisionError'>

多個except 與 finally

語法:

try:
    例外
except 例外 as 自訂名稱:
    處理例外
else:(可省略不寫)
    沒有例外發生
finally:(可省略不寫)
    不管有沒有例外都執行    

注意!以下程式碼,執行到res = num1 / num2就產生例外,之後的open("/目錄/檔案","r")就不會執行。

1
2
3
4
5
6
7
8
9
10
11
try:
    num1 = 10
    num2 = 0
    res = num1 / num2
    open("/目錄/檔案","r")
except ZeroDivisionError as e:
    print(f"ZeroDivisionError\n")
except Exception as e:
    print(f"exception: {e} \n")
finally:
    print(f"finally: \n")
ZeroDivisionError

finally:

取得多個except

1
2
3
4
5
6
7
8
9
try:
    num1 = 10
    num2 = 0
    res = num1 / num2
    open("/目錄/檔案","r")
except (ZeroDivisionError,FileNotFoundError) as e:
    print(e)
finally:
    print(f"finally: \n")
division by zero
finally:

raise

1
2
3
4
try:
    raise ZeroDivisionError("主動拋出zero例外")
except (ZeroDivisionError,FileNotFoundError) as e:
    print(f"exception: {e} type: {type(e)}")
exception: 主動拋出zero例外 type: <class 'ZeroDivisionError'>

如果沒人處理exception 由源頭處理

下方程式碼,f3() 沒有處理exception,返回f2(),但f2()也沒處理exception,返回f1(),由f1()處理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def f1():
    print("-----f1 start ----")
    try:
        f2()
    except :
        print("exception")
    print("-----f1 end ----")
def f2():
    print("-----f2 start ----")
    f3()
    print("-----f2 end ----")
def f3():
    print("-----f3 start ----")
    print(10 / 0)
    print("-----f3 end ----")

f1()
-----f1 start ----
-----f2 start ----
-----f3 start ----
exception
-----f1 end ----

Error 種類

IndexError

1
2
3
4
5
try:
    str = "Hello"
    print(str[100])
except Exception as e:
    print(e)
string index out of range

KeyError

1
2
    dict1 = {"name":"cici", "age":18}
    print(dict1["address"])

TypeError

1
2
3
4
5
6
try:
    a = "Hello"
    b = 5
    print(a + b)
except Exception as e:
    print(e)
can only concatenate str (not "int") to str

ValueError

1
2
3
4
try:
    print(int("Hello"))
except Exception as e:
    print(e)
invalid literal for int() with base 10: 'Hello'

FileNotFoundError

1
2
3
4
5
# /Users/cici/testc/file_test2
try:
    f = open("/目錄/檔名", "r", encoding="utf-8")
except Exception as e:
    print(e)
[Errno 2] No such file or directory: '/目錄/檔名'

AttributeError

沒有這個屬性

1
2
3
4
5
class A:
    def hi(self):
        pass
a = A()
print(a.name)

results matching ""

    No results matching ""