Python-try异常捕获

 

 

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')
posted @ 2019-12-26 09:29  JinweiChang  阅读(883)  评论(0编辑  收藏  举报