python中的捕獲異常的用法與c#基本相同,raise就是throw,下面2個例子函數一個系統異常,一個自定義異常。
def div(aa,bb): try: cc=aa/bb return cc except Exception as e: raise finally: print('finally') def div1(aa,bb): try: cc=aa/bb return cc except ZeroDivisionError: return '除數不能為0' except Exception: return '其他類型異常' finally: print('finally') try: kk=div1(8,0) print('try begin') print(kk) print('try end') except Exception as e: print('ex begin') print(e) print('ex end')