python 的err输出BaseExc

发布时间:2019-07-11 09:53:14编辑:auto阅读(1360)

    try:
    print('try...')
    r = 10 / 0
    print('result:', r)
    except BaseException as e:
    print('except:',e)
    finally: ##可以添加,或者删除,都会执行。

    print('finally...')

    print('END')

    此外,如果没有错误发生,可以在except语句块后面加一个else,当没有错误发生时,会自动执行else语句:

    
    try:
        print('try...')
        r = 10 / int('2')
        print('result:', r)
    except ValueError as e:
        print('ValueError:', e)
    except ZeroDivisionError as e:
        print('ZeroDivisionError:', e)
    else:
        print('no error!')
    finally:
        print('finally...')
    print('END')

    http://www.runoob.com/python/python-exceptions.html

关键字