• Python用法速查@异常处理


    Python 中的所有异常类(如ZeroDivisionError)都是从BaseException类派生的

    >>> 1/0
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ZeroDivisionError: integer division or modulo by zero
    
    

    常见捕获方式

    try...except...
    try...except...else...
    try...except...else...finally...
    try...except...except...else...finally...
    try...finally...
    
    

    try...except...

    try:
        k=1/0
    except ZeroDivisionError as e: #捕获除零异常
        print(e)
    
    print('Finish.')
    
    #结果
    
    D:pythonpython.exe E:/PYTHON/Basics/Fun/HelloYoutube.py
    division by zero
    Finish.
    
    Process finished with exit code 0
    

    try...except.../except.../...

    try:
        x = input('Enter x: ')
        y = input('Enter y: ')
        print x / y
    except ZeroDivisionError as e:      # 处理 ZeroDivisionError 异常
        print 'ZeroDivisionError:',e
    except TypeError as e:              # 处理 TypeError 异常 如 1 / 'a'
        print 'TypeError:',e
    
    print 'hello world'
    
    # 结果
    Enter x: 3
    Enter y: 'a'
    TypeError: unsupported operand type(s) for /: 'int' and 'str'
    hello world
    
    
    

    except BaseException as e:...

    捕获未知异常

    try:
        x = input('Enter x: ')
        y = input('Enter y: ')
        print x / y
    except ZeroDivisionError as e:      # 捕获 ZeroDivisionError 异常
        print 'ZeroDivisionError:',e
    except TypeError as e:              # 捕获 TypeError 异常
        print 'TypeError:',e
    except BaseException as e:          # 捕获未知异常 如 x被赋值一个回车
        print 'BaseException:',e
    
    print 'hello world'
    
    

    try...except...else...

    try:
        x = input('Enter x: ')
        y = input('Enter y: ')
        print x / y
    except ZeroDivisionError as e:
        print 'ZeroDivisionError:',e
    except TypeError as e:
        print 'TypeError:',e
    except BaseException as e:
        print 'BaseException:',e
    else:
        print 'no error!'
    
    print 'hello world'
    # 结果
    Enter x: 6
    Enter y: 2
    3
    no error!
    hello world
    
    

    try...else...finally...

    try:
        x = 1/0
        print x
    finally:
        print 'DONE'
    #结果
    
    DONE
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    ZeroDivisionError: integer division or modulo by zero
    
    
    

    raise

    try:
        x = input('Enter x: ')
        y = input('Enter y: ')
        print x / y
    except ZeroDivisionError as e:
        print 'ZeroDivisionError:',e
    except TypeError as e:
        print 'TypeError:',e
    except BaseException as e:
        print 'BaseException:',e
        raise                           # 使用 raise 抛出异常
    else:
        print 'no error!'
    
    print 'hello world'
    
    #结果
    
    Enter x:    # 这里输入回车键
    BaseException: unexpected EOF while parsing (<string>, line 0)
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "<string>", line 0
    
        ^
    SyntaxError: unexpected EOF while parsing
    
    
    

    class Exception(n)

    # 自定义异常类
    class SomeError(Exception):
        pass
    
    try:
        x = input('Enter x: ')
        y = input('Enter y: ')
        print x / y
    except ZeroDivisionError as e:
        print 'ZeroDivisionError:',e
    except TypeError as e:
        print 'TypeError:',e
    except BaseException as e:
        print 'BaseException:',e
        raise SomeError('invalid value')    # 抛出自定义的异常
    else:
        print 'no error!'
    
    print 'hello world'
    #结果
    Enter x: 
    BaseException: unexpected EOF while parsing (<string>, line 0)
    ----------------------------------------------------------------------
    SomeError                            Traceback (most recent call last)
    <ipython-input-20-66060b472f91> in <module>()
         12 except BaseException as e:
         13     print 'BaseException:',e
    ---> 14     raise SomeError('invalid value')
         15 else:
         16     print 'no error!'
    
    SomeError: invalid value
    
    

    参考

    explore-python/Exceptioon


    ________________________________________________________

    Every good deed you do will someday come back to you.

    Love you,love word !
  • 相关阅读:
    blocksit.js插件瀑布流
    防止点浏览器返回按钮,返回上一页的JS方法
    jquery 换肤功能
    HTML5获取地理位置
    百度api定位
    网站如何做到完全不需要使用jQuery
    让网页变成灰色代码
    利用html标签限制搜索引擎对网站的抓取收录
    高清ICON SVG解决方案
    加载动画
  • 原文地址:https://www.cnblogs.com/hugboy/p/Exception.html
Copyright © 2020-2023  润新知