<aside> 💡 Notion Tip: Here at Notion we use this template to help teams plan, design and develop products with the greatest chance for success. It helps teams think through their work more deeply, improves asynchronous communication with other teams, and creates space for collaboration.

</aside>

👀 錯誤/異常處理


💭 Python的例外處理

🛫 例子

my_list = [1, 2, 3, "Four", 5, 6, 7]
for element in my_list:
    result = 10 + element
    print(result)

這裡因為"Four"是一個字串,因此在走訪到這個元素時,會把+這個運算子當成串接字串的運算。因此Python不知道怎麼把它跟10這個整數型的常數做運算,而會拋出

TypeError的例外

🛫 try and xcept敘述

for element in my_list:
    try:
        result = 10 + element
        print(result)
    except:
        print("{} is not a number.".format(element))

🛫 Except敘述可以給定要接住的例外型別