1、try ...except...else結構
''' try ...except...else結構 如果try塊中沒有拋出異常,則執行else塊,如果try中拋出異常,則執行except塊 ''' try: a=int(input('請輸入被除數:')) b=int(input('請輸入除數:')) result=a/b print(result) except BaseException as e: #將錯誤命名為e print('出錯了',e) else: print('計算結果為:',result)
2、try ...except...else...finally結構
''' try ...except...else...finally結構 如果try塊中沒有拋出異常,則執行else塊,如果try中拋出異常,則執行except塊 ''' try: a=int(input('請輸入被除數:')) b=int(input('請輸入除數:')) result=a/b print(result) except BaseException as e: print('出錯了',e) else: print('計算結果為:',result) finally: # 無論出現什么情況都運行該語句 print('感謝您的應用')
3、常見異常