1. 程式人生 > >python學習筆記:try---except

python學習筆記:try---except

 一、try—except

try:
    <語句>     #正常執行語句
except:
    <語句>     #catch異常,執行語句
               #The except block tells you what to do when an exception is caught.

二、try—except Exception as e

try:
    <語句>    #正常執行語句
except Exception as e:    #catch異常,並將異常(Exception)賦值為e
    <語句>    #執行異常語句

三、try—except XXX as e

try:
    <語句>    #正常執行語句
except XXX as e:    #catchXXX型別異常,並賦值為e
    <語句>    #執行異常語句
              #XXX為Exception的某一型別,如AssertionError、ZeroDivisionError等等