• Python3 异常检测


    1、标准异常

    AssertionError

    断言语句(assert)失败

    AttributeError

    尝试访问未知的对象属性

    EOFError

    用户输入文件末尾标志EOF(Ctrl+d)

    FloatingPointError

    浮点计算错误

    GeneratorExit

    generator.close()方法被调用的时候

    ImportError

    导入模块失败的时候

    IndexError

    索引超出序列的范围

    KeyError

    字典中查找一个不存在的关键字

    KeyboardInterrupt

    用户输入中断键(Ctrl+c)

    MemoryError

    内存溢出(可通过删除对象释放内存)

    NameError

    尝试访问一个不存在的变量

    NotImplementedError

    尚未实现的方法

    OSError

    操作系统产生的异常(例如打开一个不存在的文件)

    OverflowError

    数值运算超出最大限制

    ReferenceError

    弱引用(weak reference)试图访问一个已经被垃圾回收机制回收了的对象

    RuntimeError

    一般的运行时错误

    StopIteration

    迭代器没有更多的值

    SyntaxError

    Python的语法错误

    IndentationError

    缩进错误

    TabError

    Tab和空格混合使用

    SystemError

    Python编译器系统错误

    SystemExit

    Python编译器进程被关闭

    TypeError

    不同类型间的无效操作

    UnboundLocalError

    访问一个未初始化的本地变量(NameError的子类)

    UnicodeError

    Unicode相关的错误(ValueError的子类)

    UnicodeEncodeError

    Unicode编码时的错误(UnicodeError的子类)

    UnicodeDecodeError

    Unicode解码时的错误(UnicodeError的子类)

    UnicodeTranslateError

    Unicode转换时的错误(UnicodeError的子类)

    ValueError

    传入无效的参数

    ZeroDivisionError

    除数为零

    2.内置异常类的层次结构


    BaseException
    +-- SystemExit
    +-- KeyboardInterrupt
    +-- GeneratorExit
    +-- Exception
          +-- StopIteration
          +-- ArithmeticError
          |    +-- FloatingPointError
          |    +-- OverflowError
          |    +-- ZeroDivisionError
          +-- AssertionError
          +-- AttributeError
          +-- BufferError
          +-- EOFError
          +-- ImportError
          +-- LookupError
          |    +-- IndexError
          |    +-- KeyError
          +-- MemoryError
          +-- NameError
          |    +-- UnboundLocalError
          +-- OSError
          |    +-- BlockingIOError
          |    +-- ChildProcessError
          |    +-- ConnectionError
          |    |    +-- BrokenPipeError
          |    |    +-- ConnectionAbortedError
          |    |    +-- ConnectionRefusedError
          |    |    +-- ConnectionResetError
          |    +-- FileExistsError
          |    +-- FileNotFoundError
          |    +-- InterruptedError
          |    +-- IsADirectoryError
          |    +-- NotADirectoryError
          |    +-- PermissionError
          |    +-- ProcessLookupError
          |    +-- TimeoutError
          +-- ReferenceError
          +-- RuntimeError
          |    +-- NotImplementedError
          +-- SyntaxError
          |    +-- IndentationError
          |         +-- TabError
          +-- SystemError
          +-- TypeError
          +-- ValueError
          |    +-- UnicodeError
          |         +-- UnicodeDecodeError
          |         +-- UnicodeEncodeError
          |         +-- UnicodeTranslateError
          +-- Warning
               +-- DeprecationWarning
               +-- PendingDeprecationWarning
               +-- RuntimeWarning
               +-- SyntaxWarning
               +-- UserWarning
               +-- FutureWarning
               +-- ImportWarning
               +-- UnicodeWarning
               +-- BytesWarning
               +-- ResourceWarning

    3、异常处理

    1.try语句

    try:

      检测范围

    except Exception [as reason]:

      出现Exception后处理的代码

    可以有多个except与try组合,因为检测范围中肯能会产生多个异常,可以用多个except与try组合来关注感兴趣的异常

    可以同一处理多类异常 except后要用()把多个需要同一处理的异常括起来

    try:
        int('abc')
        sum = 1+'1' 
        f = open('我是一个不存在的文档.txt')
        print(f.read())
        f.close()
    except (OSError, ValueError,TypeError)as reason:
        print('出错啦T_T
    错误原因是:' + str(reason))

    2.try finally语句

    try:

      检测范围

    except Exception [as reason]:

      出现Exception后处理的代码

    finally:

      一定要做的事情(比如出错后可以在这里把文件关闭)

    try:
        f = open('My_File.txt') # 当前文件夹中并不存在"My_File.txt"这个文件T_T
        print(f.read())
    except OSError as reason:
        print('出错啦:' + str(reason))
    finally:
        if 'f' in locals(): # 如果文件对象变量存在当前局部变量符号表的话,说明打开成功
            f.close()

    3、raise语句(引发一个异常)

    raise 异常名字

    try:
        for i in range(3):
            for j in range(3):
                if i == 2:
                    raise KeyboardInterrupt
                print(i, j)
    except KeyboardInterrupt:
        print('退出啦!')
  • 相关阅读:
    项目maven update 后启动项目出现导常:org.springframework.web.context.ContextLoaderListener
    oracle 函数
    sql 字符串函数、数学函数
    sql 内连接 子查询 合并查询
    sql 单表查询练习
    oracle 实现主键自增
    create alter rename desc select update delete insert
    oracle 数据类型
    oracle 导入导出 dmp 的三种方式
    oracle imp dmp
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/8406858.html
Copyright © 2020-2023  润新知