2019年10月31日 万能异常

 

Exception  万能异常

while True: #用while 实现循环输入
    try:
        age=input('>>')
        int(age)

        num=input('num2》》》')
        int(num)

    except ValueError as e: #把valueerror内容 取名为e,在哪个地方捕捉到异常,则会跳到except 继续往下执行
        print(e)
    except KeyError as e:
        print('keyerror',e)
    except Exception as V:  #万能异常
        print('Exception??',V)
        

 

什么时候用异常处理?!

加多了会导致可读性差。只有在异常无法预知的情况下,才应该加上 try-except 。

 

posted @ 2019-10-31 21:01  小圣庄  阅读(111)  评论(0编辑  收藏  举报